0

Step 1: get the index price from 1990-01-01 to 2015-09-16(a2), and plot the index price(a3) Step 2: add dates on the plot (dates have different labels(a4), for example,1992-12-31 has label "4L",which means stagflation) Step 3:color different areas correspond to different date's labels in the plot, that is, 1="red",2="green",3="yellow",4="blue". so, I want to fill the geom_vlines with different colors, and different colors stand for different economy cycles,which are recession, recovery, inflation, stagflation. I can't add images now,and I am horrible at describing things.I hope what I am saying makes sense to you.Sorry about this.

For example, I have date 1993-03-31, so I want to color 1992-12-31 to 1993-03-31(it has label "overheat") as red. But I really don't know how to fill the color according to dates. Thanks a lot: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.

here is a

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")
  • can you make your post reproducible [http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example] – Mateusz1981 Sep 16 '15 at 06:15
  • Please see the FAQ on how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Jaap Sep 16 '15 at 06:15
  • What is the error message? – drmariod Sep 16 '15 at 07:40
  • I gather the error is: `argument "values" is missing, with no default`?? This is because `values` is not the first argument to `scale_fill_manual(...)`, so you have to name it, as in: `scale_fill_manual(values=c(...))`. This removes the error, but doesn't solve your problem. You haven't defined a fill scale so you get no fill colors. – jlhoward Sep 16 '15 at 07:50
  • You have no aes that'smapping fill to a variable (and are you sure you want fill? Or do you want colour?) – Heroka Sep 16 '15 at 07:51
  • @Heroka I want to fill the lines inbetween with colors, like, I want to color the economy cycle with different colors, and I really don't know how to do this. – Valerie Chen Sep 16 '15 at 08:28

1 Answers1

0

Is this what you mean? (Based on the lines I filled the entire plot. Filling the area under the line is trickier). I combined data that go in the same geoms first, as that makes life a lot easier. Plot looks a bit weird, but that's because of the ranges of the given data.

#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

Heroka
  • 12,889
  • 1
  • 28
  • 38
  • If you're satisfied, can you mark the question as answered? Keeps others from spending time on it. – Heroka Sep 17 '15 at 08:38