gpt4 book ai didi

r - 将新项目添加到 ggplot2 图例

转载 作者:行者123 更新时间:2023-12-04 10:18:52 27 4
gpt4 key购买 nike

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

最佳答案

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

关于r - 将新项目添加到 ggplot2 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com