With the below code and data, I'm trying to make a faceted plot and draw a vertical line for the means by "Low" and "Middle" levels.
I also have an indicator for whether the difference between the levels is statistically significant or not, and want to color the lines as below when they are significant (note that none are significant in the example below), and otherwise they are just colored black.
I'm most of the way there, but I have myself all turned around now with the legends in particular. This is how the legend currently looks, because of the -ifelse- and incorporating the significance.
But if you remove the -ifelse- aspect to geom_vline(), the resulting legend looks like this, which is what I want:
test %>%
ggplot(aes(x = time, fill = level, color = level)) +
geom_histogram(aes(y = after_stat(density * nrow(d))),
binwidth = 10,
position = "identity",
alpha = 0.25) +
geom_vline(aes(xintercept = mean_bylevel,
color = ifelse(sig == "Sig",
level, "Not")),
show.legend = FALSE) +
facet_wrap(~condition_new) +
scale_fill_manual(values = c("Low" = "palegreen",
"Middle" = "lightpink")) +
scale_color_manual(values = c("Low" = "forestgreen",
"Middle" = "hotpink3",
"Not" = "black"))
test <- structure(list(time = c(30, 70, 20, 80, 30, 50, 50, 50, 60, 60,
30, 50, 30, 80, 60, 70, 60, 80, 80, 60, 70, 70, 20, 60, 60, 50,
30, 50, 80, 60, 40, 80, 30, 60, 50, 70, 40, 20, 30, 40, 80, 30,
70, 70, 60, 20, 40, 30, 80, 60, 70, 70, 40, 60, 40, 30, 20, 70,
60, 70, 80, 70, 40, 30, 50, 80, 20, 60, 20, 50, 30, 60, 20, 30,
70, 20, 60, 40, 70, 70, 70, 40, 40, 80, 60, 50, 60, 30, 60, 70,
30, 30, 20, 60, 80, 40, 60, 40, 20, 80), level = c("Low", "Low",
"Middle", "Low", "Low", "Middle", "Middle", "Middle", "Low",
"Low", "Middle", "Middle", "Middle", "Low", "Middle", "Low",
"Middle", "Middle", "Low", "Middle", "Low", "Low", "Low", "Middle",
"Middle", "Low", "Middle", "Middle", "Low", "Middle", "Low",
"Low", "Low", "Middle", "Middle", "Low", "Low", "Low", "Middle",
"Middle", "Middle", "Low", "Middle", "Low", "Low", "Middle",
"Middle", "Low", "Low", "Middle", "Low", "Middle", "Middle",
"Low", "Low", "Middle", "Middle", "Low", "Middle", "Low", "Middle",
"Low", "Low", "Middle", "Middle", "Low", "Low", "Middle", "Low",
"Middle", "Middle", "Low", "Low", "Middle", "Middle", "Middle",
"Low", "Middle", "Low", "Low", "Low", "Middle", "Middle", "Low",
"Low", "Low", "Middle", "Middle", "Low", "Middle", "Low", "Middle",
"Low", "Middle", "Low", "Middle", "Middle", "Low", "Low", "Low"
), mean_bylevel = c(50, 49.5, 51.2, 50.4, 50, 50.1, 49.4, 51,
50, 49.5, 49.4, 50.1, 51, 50, 51.2, 50.4, 51.2, 50.1, 50.4, 49.4,
50, 50, 49.5, 51, 51.2, 50.4, 49.4, 51, 49.5, 50.1, 50, 50, 50,
49.4, 50.1, 50.4, 49.5, 50, 51.2, 51, 50.1, 50, 51, 50.4, 50,
51.2, 49.4, 49.5, 50.4, 51.2, 50, 50.1, 49.4, 49.5, 50, 51, 51.2,
50.4, 50.1, 50, 51, 49.5, 50, 49.4, 51.2, 50, 49.5, 49.4, 50,
51, 50.1, 50.4, 50.4, 50.1, 49.4, 51.2, 50, 51, 49.5, 50, 50,
51.2, 49.4, 49.5, 50.4, 50, 50.1, 51, 50.4, 51.2, 50, 51, 49.5,
49.4, 50, 50.1, 49.4, 50, 49.5, 50.4), sig = c("Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not",
"Not", "Not", "Not", "Not", "Not", "Not", "Not", "Not"), condition_new = c("D",
"C", "A", "B", "A", "D", "C", "B", "D", "C", "C", "D", "B", "A",
"A", "B", "A", "D", "B", "C", "A", "D", "C", "B", "A", "B", "C",
"B", "C", "D", "A", "D", "D", "C", "D", "B", "C", "A", "A", "B",
"D", "D", "B", "B", "A", "A", "C", "C", "B", "A", "D", "D", "C",
"C", "A", "B", "A", "B", "D", "D", "B", "C", "A", "C", "A", "A",
"C", "C", "D", "B", "D", "B", "B", "D", "C", "A", "A", "B", "C",
"D", "A", "A", "C", "C", "B", "D", "D", "B", "B", "A", "D", "B",
"C", "C", "A", "D", "C", "D", "C", "B")), row.names = c(NA, -100L
), class = c("tbl_df", "tbl", "data.frame"))
One option to achieve your desired result would be to set the breaks=
for the color scale to include only the desired categories:
library(ggplot2)
test |>
ggplot(aes(x = time, fill = level, color = level)) +
geom_histogram(aes(y = after_stat(density * nrow(test))),
binwidth = 10,
position = "identity",
alpha = 0.25
) +
geom_vline(
aes(
xintercept = mean_bylevel,
color = ifelse(sig == "Sig", level, "Not")
),
show.legend = FALSE
) +
facet_wrap(~condition_new) +
scale_fill_manual(values = c(
"Low" = "palegreen",
"Middle" = "lightpink"
)) +
scale_color_manual(
values = c(
"Low" = "forestgreen",
"Middle" = "hotpink3",
"Not" = "black"
),
breaks = c("Low", "Middle")
)