[英]How to format number values for ggplot2 legend?
我正在努力完成使用ggplot2生成的图形,如此...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
我已附加结果,您可以看到“收入”图例中的数值没有逗号。 如何为这些值添加逗号? 我能够使用scale_y_continuous作为轴,也可以用于图例吗?
为了保持最新,在ggplot2_0.9.3中,工作语法是:
require(scales)
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(labels=comma) +
scale_colour_continuous(labels=comma)
另见这个交流
注意2014-07-16:这个答案中的语法已经过时了一段时间。 使用水杉的答案!
是的 - 只需要找到正确的scale_colour_图层。 尝试:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors, colour = Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
我个人也会将颜色映射移动到geom_point图层,这样它就不会在图例中的点后面显示奇数行:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors)) +
geom_point(aes(colour = Revenue)) +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
...当我偶然发现这个较老的线程时,也许有必要添加你需要加载library("scales")
,否则你会收到以下错误信息
Error in check_breaks_labels(breaks, labels) : object 'comma' not found
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.