Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new item to ggplot2 legend

Tags:

r

ggplot2

I am printing the Case-Shiller index in a plot:

structure(list(Date = structure(c(10957, 11048, 11139, 11231,  11323, 11413, 11504, 11596, 11688, 11778, 11869, 11961, 12053,     12143, 12234, 12326, 12418, 12509, 12600, 12692, 12784, 12874,     12965, 13057, 13149, 13239, 13330, 13422, 13514, 13604, 13695,     13787, 13879, 13970, 14061, 14153, 14245, 14335, 14426, 14518,     14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340,     15431, 15522, 15614, 15706, 10957, 11048, 11139, 11231, 11323, 
11413, 11504, 11596, 11688, 11778, 11869, 11961, 12053, 12143,     12234, 12326, 12418, 12509, 12600, 12692, 12784, 12874, 12965,     13057, 13149, 13239, 13330, 13422, 13514, 13604, 13695, 13787,     13879, 13970, 14061, 14153, 14245, 14335, 14426, 14518, 14610,     14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431,    15522, 15614, 15706), class = "Date"), 
Series = structure(c(1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L,     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
.Label = c("US Composite",    "Atlanta"), class = "factor"),
value = c(100.78, 103.42, 105.68,     108.07, 110.1, 112.36, 114.8, 116.38, 118.87, 121.93, 125.38,     128.72, 131.44, 133.9, 137.57, 142.43, 147.39, 152.61, 157.43,    163.17, 170.77, 176.33, 181.47, 187.06, 190.99, 189.46, 185.93,     186.47, 187.91, 182.52, 177.35, 170.78, 162.82, 155.1, 147.79,     139.51, 132.6, 132.16, 134.71, 136.24, 136.03, 136.89, 132.64,     131.32, 129.72, 129.22, 128.02, 126.55, 128.12, 131.2, 132.65,     135.8, 141.15, 100.37, 102.69, 104.31, 105.42, 107.06, 108.34,     109.67, 111.05, 111.66, 112.75, 113.66, 114.6, 115.65, 116.57,     117.03, 118.03, 119.3, 120.83, 121.29, 122.72, 124.64, 126.97,     127.76, 128.85, 131.71, 132.92, 133.14, 133.7, 134.98, 136.11,
134.09, 132.67, 129.7, 125.62, 121.91, 118.67, 111.48, 107.36,     106.99, 109.15, 109.35, 107.73, 106.4, 102.51, 102.69, 103.82,     100.76, 90.63, 87.55, 86.12, 90.59, 95.05, 99.4)), .Names = c("Date",  "Series", "value"), row.names = c(NA, -106L), class = "data.frame")

ggplot() + 
  geom_rect(aes(xmin=as.Date("2001-03-01"), xmax=as.Date("2001-11-30"),  ymin=-Inf, ymax=Inf),
            fill="black", alpha=0.2) +
  geom_rect(aes(xmin=as.Date("2007-12-01"), xmax=as.Date("2009-06-30"),  ymin=-Inf, ymax=Inf),
            fill="black", alpha=0.2) +
  geom_line(data=values.melted, aes(x=Date, y=value, color=Series), size=2) + 
  labs(x= "Date", y="Case-Shiller Index Value")

Case-Shiller Index This plot is perfect, except I would like to add a box to the legend indicating that the shaded areas represent US recessions. How would I do this?

like image 203
gregmacfarlane Avatar asked Aug 09 '13 20:08

gregmacfarlane


1 Answers

Arun's answer is great, and is what I would most likely do in this situation, but here's an alternative that doesn't require the second dataframe. I've combined your two geom_rect calls into one for simplicity. The key is assigning the fill to something inside aes(). It doesn't have to be in a data.frame. Then, because fill is not set to a colour value, you need to assign the colour outside geom_rect with scale_fill_manual():

ggplot() + 
  geom_rect(aes(xmin=c(as.Date("2001-03-01"),as.Date("2007-12-01")), 
                xmax=c(as.Date("2001-11-30"),as.Date("2009-06-30")),
                ymin=c(-Inf, -Inf), ymax=c(Inf, Inf),
                fill = "US Recessions"),alpha=0.2) +
  scale_fill_manual("", breaks = "US Recessions", values ="black")+
  geom_line(data=values.melted, aes(x=Date, y=value, color=Series), size=2) + 
  labs(x= "Date", y="Case-Shiller Index Value")

which gives:

enter image description here

like image 75
alexwhan Avatar answered Sep 23 '22 09:09

alexwhan