我一直在尝试使用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)), data = mdf,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", size=12, face="bold"))
我希望有人能帮助我弄清楚我可能做错了什么,并帮助绘制这些数据。
非常感谢!