无法在ggplot

我一直在尝试使用R中的ggplot2在同一图上绘制条形图和折线图,但是我无法调整辅助轴。下面是数据的样子。

Month       Domestic Visitors   Var1         Var2  Var3     Var4     Var5    Var6       Var7
2016-01-01  140018225              0          0      0        0       0        0        1500
2016-02-01  157837334              0          0      0        0       1473     0        1500
2016-03-01  153520903              0          0      0        0       114      0        1500
2016-04-01  155965472              148935   48575   11324   6075      11319    1200     19468
2016-05-01  133629040              0        74037   11324   19120     41015    1200     24468
2016-06-01  130395546              0        13500    0      12184     3311     1200     16500
2016-07-01  134910131              0          0      0        0       250      8405     1500
2016-08-01  123513886              0          0      0        0       399      8272     1500
2016-09-01  138056952              0          0      4814     0       8354     5612     5612
2016-10-01  156491556              0          0      0        0       740      7975     5724
2016-11-01  123124539              0          0      9603     0       2000     10567    8128
2016-12-01  146472839              0          0      0        0       8046     8087     4334

这就是我试图在上方绘制数据的方式

mdf <- tidyr::gather(mdf,variable,value,-Month,-one_of(colnames(mdf[2])))
ggplot() + 
  geom_bar(aes(y=mdf$value,x = as.Date(mdf$Month),fill = variable),data = mdf,stat="identity") +
  geom_line(aes(y = mdf$Domestic.Visitors,x = as.Date(mdf$Month)),stat="identity",lty = 2) +
  ggtitle("Weekly Spend Timeline") +
  labs(fill = "Channels") +
  scale_x_date(name = "Time Period",labels = NULL) +
  scale_y_continuous(sec.axis = sec_axis((~./max(mdf$Domestic.Visitors)))) +
  theme_grey() +
  theme(
    plot.title = element_text(color="black",size=16,face="bold",hjust = 0.5),axis.title.x = element_text(color="black",size=12,face="bold"),axis.title.y = element_text(color="black",face="bold"))

收集后的数据

structure(list(Month = structure(c(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,1L,12L),.Label = c("2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01","2016-09-01","2016-10-01","2016-11-01","2016-12-01"
),class = "factor"),Domestic.Visitors = c(140018225L,157837334L,153520903L,155965472L,133629040L,130395546L,134910131L,123513886L,138056952L,156491556L,123124539L,146472839L,140018225L,146472839L),variable = c("Var1","Var1","Var2","Var4","Var5","Var6","Var7","Var7"),value = c(1500L,1500L,19468L,24468L,16500L,5612L,5724L,8128L,4334L,0L,48575L,74037L,13500L,11324L,4814L,9603L,6075L,19120L,12184L,1473L,114L,11319L,41015L,3311L,250L,399L,NA,740L,2000L,8046L,1200L,8405L,8272L,8354L,7975L,10567L,8087L)),row.names = c(NA,-72L),class = "data.frame")

我希望有人能帮助我弄清楚我可能做错了什么,并帮助绘制这些数据。

非常感谢!

jinjuanyang 回答:无法在ggplot

让我们从一个简单的转换开始,将两个结合在一起。

[0,....]```

主y轴用于ggplot() + geom_bar(aes(y= value,x = as.Date(Month),fill = variable),data = mdf,stat="identity") + geom_line(aes(y = (Domestic.Visitors-100000000)/500,x = as.Date(Month)),stat="identity",lty = 2) + ggtitle("Weekly Spend Timeline") + labs(fill = "Channels") + scale_x_date(name = "Time Period",labels = NULL) + scale_y_continuous(sec.axis = sec_axis(~(.+100000000)*500)) + theme_grey() + theme( plot.title = element_text(color="black",size=16,face="bold",hjust = 0.5),axis.title.x = element_text(color="black",size=12,face="bold"),axis.title.y = element_text(color="black",face="bold")) 变量的条形图,辅助y轴用于value线。 source code

您可以调整这些值以进行变换以重新缩放次要y,例如,如果您使用120000000和300而不是100000000和500,则图形如下所示: enter image description here

本文链接:https://www.f2er.com/3098188.html

大家都在问