如何格式化ggplot2图例的数字值? [英] How to format number values for ggplot2 legend?

查看:246
本文介绍了如何格式化ggplot2图例的数字值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理像这样使用ggplot2生成的图形...

I am working on finishing up a graph generated using ggplot2 like so...

ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) 
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)

我已附上结果,您可以在图例中看到收入"中的数字值没有逗号.如何为这些值添加逗号?我能够对轴使用scale_y_continuous,也可以将其用于图例吗?

I have attached the result and you can see the numeric values in the legend for Revenue do not have a comma. How can I add a comma to those values? I was able to use scale_y_continuous for the axis, can that be used for the legend also?

推荐答案

注意2014-07-16:此答案中的语法已经过时了一段时间.使用水杉的答案!

Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!

是的-仅需弄清楚正确的scale_colour_图层即可.试试:

Yep - just a matter of getting the right scale_colour_ layer figured out. Try:

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图层,这样就不会在图例中的点后面留下奇数行:

I personally would also move my the colour mapping to the geom_point layer, so that it doesn't give you that odd line behind the dot in the legend:

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)

这篇关于如何格式化ggplot2图例的数字值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆