如何格式化 ggplot2 图例的数值?
我正在努力完成使用 ggplot2 生成的图表,如下所示...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
我已附上结果,您可以看到 Revenue 图例中的数值没有逗号。如何为这些值添加逗号?我能够使用scale_y_连续作为轴,它也可以用于图例吗?
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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了保持最新,在 ggplot2_0.9.3 中,工作语法是:
另请参阅此交换
Just to keep current, in ggplot2_0.9.3 the working syntax is:
Also see this exchange
注意2014-07-16:这个答案中的语法已经过时了一段时间。使用metasequoia的答案!
是的 - 只需找出正确的scale_colour_层即可。尝试:
我个人也会将颜色映射移动到 geom_point 图层,这样它就不会在图例中的点后面给出奇怪的线:
Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!
Yep - just a matter of getting the right scale_colour_ layer figured out. Try:
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:
...当我偶然发现这个旧线程时,也许添加您需要加载
library("scales")
是有意义的,否则您会收到以下错误消息Error in check_breaks_labels(breaks ,标签):找不到对象“逗号”
...as I stumbled over this older thread, maybe it makes sense to add you need to load
library("scales")
, otherwise you get the following error messageError in check_breaks_labels(breaks, labels) : object 'comma' not found
请注意,
comma
和comma_format()
已被label_comma()
取代。这是我通常使用的:Note that
comma
andcomma_format()
have been superceded bylabel_comma()
. Here's what I typically use: