[R] Missing shapes in legend with scale_shape_manual
Kevin Zembower
kev|n @end|ng |rom zembower@org
Mon Oct 30 21:55:17 CET 2023
Hello,
I'm trying to plot a graph of blood glucose versus date. I also record
conditions, such as missing the previous night's medications, and
missing exercise on the previous day. My data looks like:
> b2[68:74,]
# A tibble: 7 × 5
Date Time bg missed_meds no_exercise
<date> <time> <dbl> <lgl> <lgl>
1 2023-10-17 08:50 128 TRUE FALSE
2 2023-10-16 06:58 144 FALSE FALSE
3 2023-10-15 09:17 137 FALSE TRUE
4 2023-10-14 09:04 115 FALSE FALSE
5 2023-10-13 08:44 136 FALSE TRUE
6 2023-10-12 08:55 122 FALSE TRUE
7 2023-10-11 07:55 150 TRUE TRUE
>
This gets me most of the way to what I want:
ggplot(data = b2, aes(x = Date, y = bg)) +
geom_line() +
geom_point(data = filter(b2, missed_meds),
shape = 20,
size = 3) +
geom_point(data = filter(b2, no_exercise),
shape = 4,
size = 3) +
geom_point(aes(x = Date, y = bg, shape = missed_meds),
alpha = 0) + #Invisible point layer for shape mapping
scale_y_continuous(name = "Blood glucose (mg/dL)",
breaks = seq(100, 230, by = 20)
) +
geom_hline(yintercept = 130) +
scale_shape_manual(name = "Conditions",
labels = c("Missed meds",
"Missed exercise"),
values = c(20, 4),
## size = 3
)
However, the legend just prints an empty square in front of the labels.
What I want is a filled circle (shape 20) in front of "Missed meds" and
a filled circle (shape 4) in front of "Missed exercise."
My questions are:
1. How can I fix my plot to show the shapes in the legend?
2. Can my overall plotting method be improved? Would you do it this
way?
Thanks so much for your advice and guidance.
-Kevin
More information about the R-help
mailing list