如何格式化 ggplot2 图例的数值?

发布于 2024-09-28 13:16:28 字数 346 浏览 4 评论 0原文

我正在努力完成使用 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?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烛影斜 2024-10-05 13:16:28

为了保持最新,在 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)

另请参阅此交换

Just to keep current, in ggplot2_0.9.3 the working syntax is:

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)

Also see this exchange

空心↖ 2024-10-05 13:16:28

注意2014-07-16:这个答案中的语法已经过时了一段时间。使用metasequoia的答案!


是的 - 只需找出正确的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)

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:

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

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)
╄→承喏 2024-10-05 13:16:28

...当我偶然发现这个旧线程时,也许添加您需要加载 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 message

Error in check_breaks_labels(breaks, labels) : object 'comma' not found

凉城已无爱 2024-10-05 13:16:28

请注意,commacomma_format() 已被 label_comma() 取代。这是我通常使用的:

... + 
scale_y_continuous(labels = scales::label_comma()) +
scale_colour_continuous(labels = scales::label_comma()) 

Note that comma and comma_format() have been superceded by label_comma(). Here's what I typically use:

... + 
scale_y_continuous(labels = scales::label_comma()) +
scale_colour_continuous(labels = scales::label_comma()) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文