将新项添加到ggplot2图例

时间:2013-08-09 20:32:40

标签: r ggplot2

我在情节中打印Case-Shiller指数:

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 这个情节是完美的,除了我想在图例中添加一个框,表明阴影区域代表了美国经济衰退。我该怎么做?

2 个答案:

答案 0 :(得分:6)

我会创建一个新的data.frame,其中包含geom_rect的相关数据,并使用fill美学,如下所示:

dd <- structure(list(xmin = structure(c(11382, 13848), class = "Date"), 
    xmax = structure(c(11656, 14425), class = "Date"), ymin = c(-Inf, 
    -Inf), ymax = c(Inf, Inf), fill = c("a", "a")), .Names = c("xmin", 
"xmax", "ymin", "ymax", "fill"), row.names = 1:2, class = "data.frame")

> dd
        xmin       xmax ymin ymax fill
1 2001-03-01 2001-11-30 -Inf  Inf    a
2 2007-12-01 2009-06-30 -Inf  Inf    a

ggplot() + geom_rect(data=dd, aes(xmin=xmin, xmax=xmax, 
    ymin=ymin, ymax=ymax, fill=fill), 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") + 
    scale_fill_manual(name = "", values="black", label="US Recessions")

enter image description here

答案 1 :(得分:5)

Arun的答案很棒,而且在这种情况下我很可能会这样做,但这里有一个不需要第二个数据帧的替代方案。为简单起见,我将两个geom_rect调用合并为一个。关键是将填充分配给aes()内的某些内容。它不必在data.frame中。然后,由于fill未设置为颜色值,因此您需要将geom_rect以外的颜色指定为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")

给出:

enter image description here