我正在为CORE-10上的一个客户创建一个显示随时间推移的临床变化的折线图。我想删除标准图例(我在下面的代码中管理)并创建一个自定义图例,以显示图形的阴影背景区域:非临床、轻度-中度、中度-重度。我已经得到了它的地方,但似乎不能得到自定义传说,任何想法?TIA
当前代码:
library(ggplot2)
ggplot(CORE10,aes(Session, Score, colour = ID)) +
geom_point(size=5, alpha=0.3)+
geom_line(size=1)+
theme_minimal()+
scale_y_continuous(limits= c(0,40), breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40))+
scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14 ,15))+
theme(legend.position = "none")+
labs(title="Guided Self-Help CORE-10 Scores") +
geom_rect(xmin = -Inf, xmax = Inf, ymin = 0, ymax = 10, fill = "lawngreen", alpha = .01)+
geom_rect(xmin = -Inf, xmax = Inf, ymin = 10, ymax = 25, fill = "yellow", alpha = .01) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = 25, ymax = 40, fill = "red", alpha = .01) +
scale_fill_manual(name= "Legend", breaks=c("lawngreen", "yellow", "red" ),
values= c("lawngreen", "yellow", "red"),
labels=c("Non-Clinical", "Mild-Moderate", "Moderate-Severe"))
数据
CORE10 <- data.frame(
ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
Session = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
Score = c(23, 11, 23, 12, 6, 12, 4, 24, 7, 7, 4, 3, 18, 8, 4)
)
1条答案
按热度按时间h79rfbju1#
用矩形的坐标和图例标签创建一个新的data.frame。仅在对
geom_rect
的一次调用中将其用作data
参数。然后,在scale_fill_manual
中,使用命名向量legend_colors
将颜色Map到图例标签。创建于2023-06-23带有reprex v2.0.2
如果你想在矩形周围画黑线,可以在
geom_rect
上加上color
和linewidth
。创建于2023-06-23带有reprex v2.0.2