geom_rect命名自定义图例

fruv7luv  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在为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)
)
h79rfbju

h79rfbju1#

用矩形的坐标和图例标签创建一个新的data.frame。仅在对geom_rect的一次调用中将其用作data参数。然后,在scale_fill_manual中,使用命名向量legend_colors将颜色Map到图例标签。

library(ggplot2)

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

rects <- data.frame(
  xmin = c(-Inf, -Inf, -Inf),
  xmax = c(Inf, Inf, Inf),
  ymin = c(0, 10, 25),
  ymax = c(10, 25, 40),
  labels = c("Non-Clinical", "Mild-Moderate", "Moderate-Severe")
)

legend_colors <- setNames(c("lawngreen", "yellow", "red"), rects$labels)

ggplot(CORE10, aes(Session, Score, colour = ID)) +
  geom_point(size = 5, alpha = 0.3, show.legend = FALSE)+
  geom_line(linewidth = 1, show.legend = FALSE)+
  geom_rect(
    data = rects,
    mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = labels),
    alpha = 0.1,
    inherit.aes = FALSE
  ) +
  scale_y_continuous(limits= c(0,40), breaks = c(0, 40, 5))+
  scale_x_continuous(breaks = 1:15)+
  scale_fill_manual(name = "Legend", values = legend_colors) +
  labs(title="Guided Self-Help CORE-10 Scores") +
  theme_minimal()

创建于2023-06-23带有reprex v2.0.2
如果你想在矩形周围画黑线,可以在geom_rect上加上colorlinewidth

ggplot(CORE10, aes(Session, Score, colour = ID)) +
  geom_point(size = 5, alpha = 0.3, show.legend = FALSE)+
  geom_line(linewidth = 1, show.legend = FALSE)+
  geom_rect(
    data = rects,
    mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = labels),
    color = "black", 
    linewidth = 0.5,
    alpha = 0.1,
    inherit.aes = FALSE
  ) +
  scale_y_continuous(limits= c(0,40), breaks = c(0, 40, 5))+
  scale_x_continuous(breaks = 1:15)+
  scale_fill_manual(name = "Legend", values = legend_colors) +
  labs(title="Guided Self-Help CORE-10 Scores") +
  theme_minimal()

创建于2023-06-23带有reprex v2.0.2

相关问题