将新项目添加到ggplot2图例 [英] Add new item to ggplot2 legend

查看:89
本文介绍了将新项目添加到ggplot2图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打印Case-Shiller索引:

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")


这个图是完美的,除了我想在图例中添加一个框来表示阴影区域代表美国经济衰退。我将如何做到这一点?

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?

推荐答案

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

I'd create a new data.frame containing the relevant data for the geom_rect's and use the fill aesthetic as follows:

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")

这篇关于将新项目添加到ggplot2图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆