如何在ggplot2中的条形图上叠加折线图 [英] How to superimpose a line graph on a barplot in ggplot2
本文介绍了如何在ggplot2中的条形图上叠加折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
df2
Year variable value pos
1 2001-04-01 Purchase 4471.05 TRUE
2 2002-04-01 Purchase 5084.00 TRUE
3 2003-04-01 Purchase 0.00 TRUE
4 2004-04-01 Purchase 0.00 TRUE
5 2005-04-01 Purchase 0.00 TRUE
6 2006-04-01 Purchase 740.00 TRUE
7 2007-04-01 Purchase 720.00 TRUE
8 2008-04-01 Purchase 13510.00 TRUE
9 2009-04-01 Purchase 104480.34 TRUE
10 2010-04-01 Purchase 85399.39 TRUE
11 2011-04-01 Purchase 78799.05 TRUE
12 2012-04-01 Purchase 142271.84 TRUE
13 2013-04-01 Purchase 164240.00 TRUE
14 2014-04-01 Purchase 54579.00 TRUE
15 2015-04-01 Purchase 10.00 TRUE
16 2016-04-01 Purchase 88124.00 TRUE
17 2001-04-01 Sale 23795.10 TRUE
18 2002-04-01 Sale 35418.45 TRUE
19 2003-04-01 Sale 53401.52 TRUE
20 2004-04-01 Sale 41598.01 TRUE
21 2005-04-01 Sale 2899.24 TRUE
22 2006-04-01 Sale 4652.71 TRUE
23 2007-04-01 Sale 5845.26 TRUE
24 2008-04-01 Sale 7587.17 TRUE
25 2009-04-01 Sale 9932.03 TRUE
26 2010-04-01 Sale 9931.45 TRUE
27 2011-04-01 Sale 11574.75 TRUE
28 2012-04-01 Sale 8186.81 TRUE
29 2013-04-01 Sale 10737.00 TRUE
30 2014-04-01 Sale 2577.00 TRUE
31 2015-04-01 Sale 63428.00 TRUE
32 2016-04-01 Sale 35800.00 TRUE
33 2001-04-01 Net -19324.05 FALSE
34 2002-04-01 Net -30334.59 FALSE
35 2003-04-01 Net -53401.52 FALSE
36 2004-04-01 Net -41598.01 FALSE
37 2005-04-01 Net -2899.24 FALSE
38 2006-04-01 Net -3912.71 FALSE
39 2007-04-01 Net -5125.38 FALSE
40 2008-04-01 Net 5922.83 TRUE
41 2009-04-01 Net 94548.32 TRUE
42 2010-04-01 Net 75467.93 TRUE
43 2011-04-01 Net 67224.30 TRUE
44 2012-04-01 Net 134086.03 TRUE
45 2013-04-01 Net 153503.00 TRUE
46 2014-04-01 Net 16675.00 TRUE
47 2015-04-01 Net -63418.00 FALSE
48 2016-04-01 Net 52324.00 TRUE
为第33至48行创建了一个并排的barplot:
Created a side by side barplot for rows 33 to 48:
library(ggplot2) #development version
p<-ggplot(df2[33:48,], aes(x=Year, y=value, fill=pos)) +
geom_bar(stat='identity', position='identity') + ggtitle("OMO (Rs'crores)\n") + theme(plot.title = element_text(lineheight=.8, face="bold",colour = "darkred",hjust =0.5,vjust = 2,size = 9)) + labs(x = "\nYear", y = "Rs crores\n",caption="Sources:Data is from RBI site")+scale_y_continuous(limits = c(-65000,200000),breaks =c(-60000,-30000,0,30000,60000,90000,120000,150000,180000,180000)) + scale_x_date(date_breaks = "2 year",date_labels = "%Y")
p
我想将1至16行和17至32行添加(叠加)为上述条形图上的2个单独的线图:
I want to add (overlay) the rows 1 to 16 and 17 to 32 as 2 separate line plots on the above bargraph:
我尝试过:
添加第一行(variable =="purchase")p + geom_line(df2 [1:16],aes(x = Year,y = value))错误:ggplot2不知道如何处理uneval类的数据
Adding the first line (variable=="purchase") p + geom_line(df2[1:16],aes(x=Year, y=value)) Error: ggplot2 doesn't know how to deal with data of class uneval
我要去哪里错了?
推荐答案
我认为这与您想要的内容有关.我重新排列了数据框,并稍微调整了值,并为这些行添加了图例.
I think this is about what you want. I rearranged the dataframe, and adjusted the values a bit, and added a legend for the lines.
library(ggplot2)
df2 <- read.table(stringsAsFactors=F,text=
"Year variable value pos
1 2001-04-01 Purchase 4471.05 TRUE
2 2002-04-01 Purchase 5084.00 TRUE
3 2003-04-01 Purchase 0.00 TRUE
4 2004-04-01 Purchase 0.00 TRUE
5 2005-04-01 Purchase 0.00 TRUE
6 2006-04-01 Purchase 740.00 TRUE
7 2007-04-01 Purchase 720.00 TRUE
8 2008-04-01 Purchase 13510.00 TRUE
9 2009-04-01 Purchase 104480.34 TRUE
10 2010-04-01 Purchase 85399.39 TRUE
11 2011-04-01 Purchase 78799.05 TRUE
12 2012-04-01 Purchase 142271.84 TRUE
13 2013-04-01 Purchase 164240.00 TRUE
14 2014-04-01 Purchase 54579.00 TRUE
15 2015-04-01 Purchase 10.00 TRUE
16 2016-04-01 Purchase 88124.00 TRUE
17 2001-04-01 Sale 23795.10 TRUE
18 2002-04-01 Sale 35418.45 TRUE
19 2003-04-01 Sale 53401.52 TRUE
20 2004-04-01 Sale 41598.01 TRUE
21 2005-04-01 Sale 2899.24 TRUE
22 2006-04-01 Sale 4652.71 TRUE
23 2007-04-01 Sale 5845.26 TRUE
24 2008-04-01 Sale 7587.17 TRUE
25 2009-04-01 Sale 9932.03 TRUE
26 2010-04-01 Sale 9931.45 TRUE
27 2011-04-01 Sale 11574.75 TRUE
28 2012-04-01 Sale 8186.81 TRUE
29 2013-04-01 Sale 10737.00 TRUE
30 2014-04-01 Sale 2577.00 TRUE
31 2015-04-01 Sale 63428.00 TRUE
32 2016-04-01 Sale 35800.00 TRUE
33 2001-04-01 Net -19324.05 FALSE
34 2002-04-01 Net -30334.59 FALSE
35 2003-04-01 Net -53401.52 FALSE
36 2004-04-01 Net -41598.01 FALSE
37 2005-04-01 Net -2899.24 FALSE
38 2006-04-01 Net -3912.71 FALSE
39 2007-04-01 Net -5125.38 FALSE
40 2008-04-01 Net 5922.83 TRUE
41 2009-04-01 Net 94548.32 TRUE
42 2010-04-01 Net 75467.93 TRUE
43 2011-04-01 Net 67224.30 TRUE
44 2012-04-01 Net 134086.03 TRUE
45 2013-04-01 Net 153503.00 TRUE
46 2014-04-01 Net 16675.00 TRUE
47 2015-04-01 Net -63418.00 FALSE
48 2016-04-01 Net 52324.00 TRUE")
# Rearrange the dataframe
df2$Year <- as.Date(df2$Year)
df3 <- df2[33:48,]
df3$sales <- df2[1:16,"value"]
df3$purchase <- df2[17:32,"value"]
# NOw plot it
p<-ggplot(df3, aes(x=Year, y=value)) +
# The bars
geom_bar(aes(fill=pos),stat='identity', position='identity') +
scale_fill_manual(name="Positive",values=c("TRUE"="gray30","FALSE"="darkred")) +
# The Lines
geom_line(aes(x=Year,y=purchase,color="purchase"),size=2) +
geom_line(aes(x=Year,y=sales,color="sales"),size=2) +
scale_y_continuous(limits = c(-65000,200000), breaks = 30000*(-2:6)) +
scale_x_date(date_breaks = "2 year",date_labels = "%Y") +
scale_colour_manual(name="Value",values =c("purchase"="darkred","sales"="darkblue")) +
ggtitle("OMO (Rs'crores)\n") +
theme(plot.title = element_text(lineheight=.8, face="bold",colour = "darkred",
hjust =0.5,vjust = 2,size = 9)) +
labs(x = "\nYear", y = "Rs crores\n",caption="Sources:Data is from RBI site")
p
提供此:
这篇关于如何在ggplot2中的条形图上叠加折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文