ggplot2使用scale_fill_manual将匹配区域应用于不同的颜色

时间:2015-09-16 06:04:15

标签: r ggplot2

步骤1:获取指数价格从1990-01-01到2015-09-16(a2),并绘制指数价格(a3) 第2步:在图上添加日期(日期有不同的标签(a4),例如,1992-12-31标签为“4L”,表示滞胀) 步骤3:颜色不同的区域对应于图中不同日期的标签,即1 =“红色”,2 =“绿色”,3 =“黄色”,4 =“蓝色”。 所以,我想用不同的颜色填充geom_vlines,不同的颜色代表不同的经济周期,即衰退,复苏,通货膨胀,滞胀。 我现在无法添加图像,而且我在描述事物时非常糟糕。我希望我所说的对你有意义。对此有所了解。

例如,我的日期是1993-03-31,所以我想将1992-12-31到1993-03-31(它的标签“过热”)颜色为红色。但我真的不知道如何根据日期填充颜色。非常感谢:D

m.p<-qplot(x=a2,y=a3,geom="line") #a2 is the date, a3 is the stock price
yrng<-range(a3)
xrng<-range(a2)
date<-read.table("clipboard",colClasses = "numeric") #adding the dates       
date4<-data.matrix(date)
date4<-as.numeric(date4)
m.p+geom_vline(aes(xintercept=date4)) #adding the designated dates
+scale_fill_manual(value=c("blue","red","green","yellow") #I know this is wrong,   but I really don't know how to write the code.

这是一个

dput(head(a2,100))
structure(c(7657, 7658, 7659, 7662, 7663, 7664, 7665, 7666, 7669, 
7671, 7672, 7673, 7676, 7677, 7678, 7679, 7680, 7683, 7684, 7685, 
7686, 7687, 7690, 7691, 7692, 7693, 7694, 7697, 7698, 7699, 7700, 
7701, 7704, 7705, 7706, 7707, 7708, 7711, 7712, 7713, 7714, 7719, 
7720, 7721, 7722, 7725, 7726, 7727, 7728, 7729, 7732, 7733, 7734, 
7735, 7736, 7739, 7740, 7741, 7742, 7743, 7746, 7747, 7748, 7749, 
7750, 7753, 7754, 7755, 7756, 7757, 7760, 7761, 7762, 7763, 7764, 
7767, 7768, 7769, 7770, 7771, 7774, 7775, 7776, 7777, 7778, 7781, 
7782, 7783, 7784, 7785, 7788, 7789, 7791, 7792, 7795, 7796, 7797, 
7798, 7799, 7802), class = "Date")

  dput(head(a3,100))
c(99.98, 104.39, 109.13, 114.55, 120.25, 125.27, 125.28, 126.45, 
127.61, 128.84, 130.14, 131.44, 132.06, 132.68, 133.34, 133.97, 
134.6, 134.67, 134.74, 134.24, 134.25, 134.24, 134.24, 133.72, 
133.17, 132.61, 132.05, 131.46, 130.95, 130.44, 129.97, 129.51, 
129.05, 128.58, 129.14, 129.79, 130.38, 130.97, 131.35, 131.92, 
132.53, 133.13, 133.67, 134.28, 134.87, 134.4, 133.93, 133.47, 
133.01, 132.53, 131.99, 131.46, 130.94, 130.41, 129.89, 129.31, 
128.77, 126.15, 125.63, 125.17, 124.71, 124.22, 123.66, 123.12, 
122.62, 122.12, 121.62, 121.12, 120.61, 120.19, 120.73, 121.21, 
121.71, 121.72, 121.54, 121.09, 120.62, 120.16, 119.64, 119.21, 
118.77, 118.34, 117.9, 117.45, 117.08, 116.63, 116.19, 115.79, 
115.36, 115.56, 114.75, 113.94, 113.16, 112.41, 111.61, 110.82, 
110.03, 109.29, 108.53, 107.84)

dput(head(date,100))
structure(list(V1 = structure(c(8400, 8490, 8581, 8765, 8855, 
8946, 9038, 9220, 9495, 9677, 9861, 9951, 10499, 10591, 10681, 
10864, 11047, 11230, 11412, 11503, 11595, 11777, 11868, 11960, 
12052, 12142, 12233, 12325, 12417, 12508, 12691, 12783, 12873, 
12964, 13148, 13238, 13329, 13421, 13513, 13786, 14060, 14425, 
14517, 14790, 14974, 15155, 15339, 15705, 15795, 15886, 15978, 
16070, 16160, 16251, 16343, 16616), class = "Date")), .Names = "V1", row.names = c(NA, 
56L), class = "data.frame")

dput(head(a4,100))
structure(list(V1 = structure(c(4L, 1L, 4L, 1L, 4L, 3L, 4L, 3L, 
2L, 3L, 2L, 3L, 2L, 1L, 3L, 4L, 1L, 4L, 2L, 4L, 3L, 2L, 3L, 1L, 
4L, 1L, 4L, 1L, 4L, 1L, 4L, 3L, 2L, 3L, 1L, 2L, 1L, 3L, 1L, 4L, 
3L, 2L, 1L, 4L, 1L, 4L, 3L, 1L, 4L, 3L, 1L, 4L, 3L, 2L, 3L, 4L
), .Label = c("overheat", "recovery", "reflation", "stagflation"
), class = "factor")), .Names = "V1", row.names = c(NA, 56L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

这是什么意思? (根据我填写整个地块的线条。填充线下的区域比较棘手)。我首先将数据放在同一个geoms中,因为这样可以让生活变得更轻松。情节看起来有点奇怪,但那是因为给定数据的范围。

#create combined data
stock_prices <- data.frame(price=a2,date=a3)
#note that I have renamed date to date_, don't name variables after base functions
economic_periods <- data.frame(date=date_$V1, period=a4$V1)
#create end-dates in new column, defined as start-date from previous row except
#for last date.
economic_periods$end_date <- c(economic_periods$date[-1],max(economic_periods$date)+1)       

p1 <- ggplot() +
  geom_line(data=stock_prices, aes(x=a2,y=a3))
p1 + geom_rect(data=economic_periods, aes(xmin=date,xmax=end_date,ymin=-Inf,ymax=Inf,fill=period),alpha=0.3)

enter image description here