?s 20:55 de 30/10/2023, Kevin Zembower via R-help
escreveu:> 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
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,
In ggplot2 graphics when you have more than one call to the same layer
function, then you can probably simplify the code.
In this case you make several calls to geom_point. This can probably be
avoided.
Create a new column named Condition.
Assign to it the column names wherever the values of those columns are
TRUE. The simplest way of doing this is to use colus missed_meds and
no_exercise as logical index columns, see code below.
Like this the values are mapped to shapes in just one call to geom_point.
That's what function aes() is meant for, to tell what variables define
what in the plot.
b2$Date <- as.Date(b2$Date)
# this new column will be mapped to the shape aesthetic
b2$Conditions <- NA_character_
b2$Conditions[b2$missed_meds] <- names(b2)[4]
b2$Conditions[b2$no_exercise] <- names(b2)[5]
ggplot(data = b2, aes(x = Date, y = bg)) +
geom_line() +
geom_point(aes(shape = Conditions), size = 3) +
geom_hline(yintercept = 130) +
scale_y_continuous(
name = "Blood glucose (mg/dL)",
breaks = seq(100, 230, by = 20)
) +
scale_shape_manual(
#name = "Conditions",
labels = c("Missed meds", "Missed exercise"),
values = c(20, 4),
na.translate = FALSE
)
Hope this helps,
Rui Barradas
--
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a
de v?rus.
www.avg.com