1

I'm creating a line graph showing clinical change over time for a client on the CORE-10. I want to remove the standard legend(which I've managed in the code below) and create a custom legend to show the shaded background area of my graph as: non-clinical, mild-moderate, moderate-severe. I've gotten somewhere with it, but can't seem to get the custom legend, any thoughts? TIA

Current graph

My current code:

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

Data

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)
)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
mindyeti
  • 11
  • 2
  • 1
    Please include a sample of your data by pasting the output of `dput(CORE10)` into the question: it need only be the first few data points. – Peter Jun 23 '23 at 13:45
  • Does this help?'structure(list(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)) – mindyeti Jun 23 '23 at 13:56
  • 1
    Replace `fill = "lawngreen"` with `aes(fill = "Non-Clinical")` in the first rectangle layer, `aes(fill = "Mild-Moderate")` in the second and `aes(fill = "Moderate-Severe")` in the third. Change your scale breaks to what you have now for labels, and that should get you a step closer. – teunbrand Jun 23 '23 at 14:00

1 Answers1

2

Create a new data.frame with the rectangles' coordinates and the legend labels. Use it as the data argument in one call to geom_rect only. Then, in scale_fill_manual, map the colors to the legend labels with the named vector legend_colors.

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

Created on 2023-06-23 with reprex v2.0.2


If you want the black lines around the rectangles, add color and linewidth to geom_rect.

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

Created on 2023-06-23 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66