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行创建了并排条形图:
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个单独的线图:
我试过了:
添加第一行(变量==“购买”) p + geom_line(df2 [1:16],aes(x =年,y =值)) 错误:ggplot2不知道如何处理class uneval的数据
我哪里错了?
答案 0 :(得分:1)
我认为这是关于你想要的。我重新排列了数据帧,并稍微调整了一下值,并为这些行添加了一个图例。
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
产生这个:
答案 1 :(得分:0)
重写上述解决方案以使其正确
重新排列数据框
df2$Year <- as.Date(df2$Year)
df3 <- df2[33:48,]
df3$purchase <- df2[1:16,"value"]
df3$sales<- df2[17:32,"value"]
df3
没有情节
p<-ggplot(df3, aes(x=Year, y=value)) +
酒吧
geom_bar(aes(fill=pos),stat='identity', position='identity') + scale_fill_manual(name="Positive",values=c("TRUE"="#00BFC4","FALSE"="#F8766D")) + theme(panel.grid.minor = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(panel.grid.major = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(plot.background = element_rect(fill="#e0edbd" )) + theme(panel.background = element_rect(fill = "#fbfefe")) + scale_fill_discrete(name="",breaks=c("TRUE","FALSE"),labels=c("Net Purchases(+)","Net Sales(-)")) +
线条
geom_line(aes(x=Year,y=purchase,color="purchase"),size=1.35) + geom_line(aes(x=Year,y=sales,color="sales"),size=1.35) + 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="",values=c("#0072B2","#D55E00"),breaks=c("purchase","sales")) + ggtitle("Open Market Operation(OMO) since 2001 (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") + theme(plot.caption=element_text(size=8, hjust=1.4, margin=margin(t=10)))