如果我已经有了y比例,请使用逗号(和K?MM?)格式化ggplot2轴标签 [英] Formatting ggplot2 axis labels with commas (and K? MM?) if I already have a y-scale

查看:153
本文介绍了如果我已经有了y比例,请使用逗号(和K?MM?)格式化ggplot2轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为ggplot图的y轴标签设置成本和收入(均以千计)和印象数(以百万计)的格式.

I am trying to format Cost and Revenue (both in thousands) and Impressions (in millions) data for a ggplot graph's y-axis labels.

我的绘图从31天前开始运行到昨天",并使用该时间段内的最小值和最大值作为ylim(c(min,max))选项.仅显示费用"示例,

My plot runs from 31 days ago to 'yesterday' and uses the min and max values over that period for the ylim(c(min,max)) option. Showing just the Cost example,

library(ggplot2)
library(TTR)

set.seed(1984)

#make series
start <- as.Date('2016-01-01')
end <- Sys.Date()

days <- as.numeric(end - start)

#make cost and moving averages
cost <- rnorm(days, mean = 45400, sd = 11640)
date <- seq.Date(from = start, to = end - 1, by = 'day') 
cost_7 <- SMA(cost, 7)
cost_30 <- SMA(cost, 30)

df <- data.frame(Date = date, Cost = cost, Cost_7 = cost_7, Cost_30 = cost_30)


# set parameters for window
left <- end - 31
right <- end - 1

# plot series
ggplot(df, aes(x = Date, y = Cost))+
geom_line(lwd = 0.5) +
geom_line(aes(y = Cost_7), col = 'red', linetype = 3, lwd = 1) +
geom_line(aes(y = Cost_30), col = 'blue', linetype = 5, lwd = 0.75) +
xlim(c(left, right)) + 
ylim(c(min(df$Cost[df$Date > left]), max(df$Cost[df$Date > left]))) +
xlab("")

我想a)用逗号表示y轴上的成千上万,b)像那些缩写的数字,用"K"表示数千或"MM"表示百万.我意识到b)可能是一个艰巨的任务,但目前a)无法通过

I would a) like to represent thousands and millions on the y-axis with commas, and b) like those numbers abbreviated and with 'K' for thousands or 'MM' for millions. I realize b) may be a tall order, but for now a) cannot be accomplished with

ggplot(...) + ... + ylim(c(min, max)) + scale_y_continuous(labels = comma)

因为引发了以下错误:

## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.

我尝试将scale_y_continuous(labels = comma)部分放在geom_line()层之后(上面抛出错误)或所有ggplot层的末尾,这会覆盖我在ylim调用中的限制,然后抛出错误无论如何,上面.

I have tried putting the scale_y_continuous(labels = comma) section after the geom_line()layer (which throws the error above) or at the end of all the ggplot layers, which overrides my limits in the ylim call and then throws the error above, anyway.

有什么想法吗?

推荐答案

对于逗号格式,您需要包括label=commascales库.您讨论的错误"实际上只是一个警告,因为您同时使用了ylimscale_y_continuous.第二个呼叫优先于第一个.您可以设置限制,并在一次调用scale_y_continuous的地方指定逗号分隔的标签:

For the comma formatting, you need to include the scales library for label=comma. The "error" you discussed is actually just a warning, because you used both ylim and then scale_y_continuous. The second call overrides the first. You can instead set the limits and specify comma-separated labels in a single call to scale_y_continuous:

library(scales)

ggplot(df, aes(x = Date, y = Cost))+
  geom_line(lwd = 0.5) +
  geom_line(aes(y = Cost_7), col = 'red', linetype = 3, lwd = 1) +
  geom_line(aes(y = Cost_30), col = 'blue', linetype = 5, lwd = 0.75) +
  xlim(c(left, right)) + 
  xlab("") +
  scale_y_continuous(label=comma, limits=c(min(df$Cost[df$Date > left]), 
                                           max(df$Cost[df$Date > left])))

另一种选择是在绘制之前将数据融化为长格式,从而减少所需的代码量并简化美学映射:

Another option would be to melt your data to long format before plotting, which reduces the amount of code needed and streamlines aesthetic mappings:

library(reshape2)

ggplot(melt(df, id.var="Date"), 
       aes(x = Date, y = value, color=variable, linetype=variable))+
  geom_line() +
  xlim(c(left, right)) + 
  labs(x="", y="Cost") +
  scale_y_continuous(label=comma, limits=c(min(df$Cost[df$Date > left]), 
                                           max(df$Cost[df$Date > left])))

无论哪种方式,将y值设置为数千或数百万,您都可以将y值除以1,000或1,000,000.我在下面使用了dollar_format(),但是我认为如果使用unit_format(根据@joran的建议),还需要除以适当的10的幂.例如:

Either way, to put the y values in terms of thousands or millions you could divide the y values by 1,000 or 1,000,000. I've used dollar_format() below, but I think you'll also need to divide by the appropriate power of ten if you use unit_format (per @joran's suggestion). For example:

div=1000

ggplot(melt(df, id.var="Date"), 
       aes(x = Date, y = value/div, color=variable, linetype=variable))+
  geom_line() +
  xlim(c(left, right)) + 
  labs(x="", y="Cost (Thousands)") +
  scale_y_continuous(label=dollar_format(),
                     limits=c(min(df$Cost[df$Date > left]), 
                              max(df$Cost[df$Date > left]))/div)

根据需要使用scale_color_manualscale_linetype_manual设置自定义颜色和线型.

Use scale_color_manual and scale_linetype_manual to set custom colors and linetypes, if desired.

这篇关于如果我已经有了y比例,请使用逗号(和K?MM?)格式化ggplot2轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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