首页 > 解决方案 > 如何将 % 符号添加到 ggplot

问题描述

我无法将 % 符号添加到 ggplot (R) (折线图)

这是我的情节代码:

ggplot(table)  + 
    geom_bar(aes(x=year, y=order_cnt_in_thousends),stat="identity", fill="tan1", colour="sienna3")+
    geom_line(aes(x=year, y=yearly_change ),stat="identity")+
    geom_text(aes(label=yearly_change, x=year, y=yearly_change), colour="black")+
    geom_text(aes(label=order_cnt_in_thousends, x=year, y=order_cnt_in_thousends), colour="black", vjust = -0.5) +   scale_x_date(date_breaks = "1 year")

我试图将它添加到列中: table$yearly_change <-paste(year_growth_table$yearly_change, "%")但现在我遇到了问题,因为它是离散的而不是连续的。

绘图图像

标签: rggplot2

解决方案


您可以使用 中的paste函数aes()使其成为带有 "%" 的字符串:

set.seed(1234)
df = data.frame(year=as.Date(c("2016-01-01","2017-01-01","2018-01-01")),order_cnt_in_thousends = sample(1000,3),yearly_change = sample(1000,3))

ggplot(df)  + 
geom_bar(aes(x=year, y=order_cnt_in_thousends),stat="identity", fill="tan1", colour="sienna3")+
geom_line(aes(x=year, y=yearly_change ))+
geom_text(aes(label=paste(yearly_change,"%"), x=year, y=yearly_change), colour="black")+
geom_text(aes(label=order_cnt_in_thousends, x=year, y=order_cnt_in_thousends), colour="black", vjust = -0.5) +   scale_x_date(date_breaks = "1 year")

在此处输入图像描述


推荐阅读