问题
I have been making this scatterplot of means in R and I know I should include all variables of interest as an aesthetic in the figure plot. However, in the figure that I created (see below), the legend doesn't show.
The variables that I'm interested in are the means, errorbars and the coloured rectangles. Anyone knows how to plot them in a smart or manual manner?
df <- data.frame(weeks = c(-1, 0, 1, 2, 3, 4),
mean = c(64, 65, 66, 66, 66, 67),
lowerCI = c(63.4, 64.9, 64.5, 63.8, 62.1, 66.8),
upperCI = c(65.6, 65.1, 66.5, 67.2, 68.9, 67.2))
sp_ts <- ggplot(data = df,
aes(x = weeks,
y = mean))
sp_ts +
geom_point(shape = 15,
size = 4) +
geom_errorbar(aes(ymin = lowerCI,
ymax = upperCI),
width = 0.05,
size = 0.5) +
labs(title = "Scatterplot of means",
x = "Weeks",
y = "Means") +
scale_x_continuous(limits = c(-1, 4),
breaks = c(-1, 0, 1, 2, 3, 4)) +
scale_y_continuous(limits = c(62, 69),
breaks = c(61, 62, 63, 64, 65, 66, 67, 68, 69)) +
annotate("rect", xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf, fill = "light blue", alpha = 0.2) +
annotate("rect", xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "blue", alpha = 0.2) +
theme_bw() +
theme(panel.grid.minor = element_blank())
回答1:
Here is one way to show the legend (taken from this answer)
library(ggplot2)
sp_ts1 <- sp_ts +
geom_point(shape = 15, size = 4) +
geom_errorbar(aes(ymin = lowerCI, ymax = upperCI),
width = 0.05,
size = 0.5) +
labs(title = "Scatterplot of means",
x = "Weeks",
y = "Means") +
scale_x_continuous(limits = c(-1, 4),
breaks = c(-1, 0, 1, 2, 3, 4)) +
scale_y_continuous(limits = c(62, 69),
breaks = c(61, 62, 63, 64, 65, 66, 67, 68, 69)) +
annotate("rect", xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf,
fill = "light blue", alpha = 0.2) +
annotate("rect", xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf,
fill = "blue", alpha = 0.2) +
theme_bw() +
theme(panel.grid.minor = element_blank())
sp_ts2 <- sp_ts1 +
geom_point(aes(color = "Mean"), shape = 15, size = 4) +
geom_errorbar(aes(ymin = lowerCI, ymax = upperCI,
color = "95% CI"),
width = 0.05,
size = 0.5) +
scale_color_manual(name = "Legend", values = c("#666666", "#1B9E77")) +
guides(colour = guide_legend(override.aes = list(linetype = c("solid", "blank"),
shape = c(NA, 15))))
sp_ts2

To show 95% CI as a vertical line, use geom_linerange
:
sp_ts3 <- sp_ts1 +
geom_point(aes(color = "Mean"), shape = 15, size = 4) +
geom_linerange(aes(ymin = lowerCI, ymax = upperCI,
color = "95% CI")) +
scale_color_manual(name = "Legend", values = c("#666666", "#1B9E77")) +
guides(colour = guide_legend(override.aes = list(linetype = c("solid", "blank"),
shape = c(NA, 15))))
sp_ts3

Edit: To show the filled rectangles, we need to use geom_rect
instead of annotate
sp_ts3 +
geom_rect(aes(fill = "First"), xmin = -Inf, xmax = 2, ymin = -Inf, ymax = Inf,
alpha = 0.1) +
geom_rect(aes(fill = "Second"), xmin = 2, xmax = Inf, ymin = -Inf, ymax = Inf,
alpha = 0.1) +
scale_fill_manual(name = "Group",
values = c(`First` = "bisque", `Second` = "cornflowerblue")) +
guides(fill = guide_legend(override.aes= list(alpha = 0.6)))

Created on 2018-04-04 by the reprex package (v0.2.0).
来源:https://stackoverflow.com/questions/49644149/legend-ggplot-figure-doesnt-show-for-means-errorbars-and-coloured-rectangle