图例ggplot图未显示均值,错误栏和彩色矩形 [英] Legend ggplot figure doesn't show for means, errorbars and coloured rectangle
问题描述
我一直在用R绘制均值散点图,我知道我应该在图形图中包括所有感兴趣的变量,以使其美观.但是,在我创建的图中(参见下文),图例没有显示.
我感兴趣的变量是平均值,误差线和彩色矩形.有谁知道如何以智能或手动方式绘制它们?
df<-data.frame(周= c(-1,0,1,2,3,4),平均值= c(64,65,66,66,66,67),下CI = 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 =星期,y =平均值))sp_ts +geom_point(形状= 15,大小= 4)+geom_errorbar(aes(ymin = lowerCI,ymax = upperCI),宽度= 0.05,大小= 0.5)+实验室(标题=均值散点图",x = "周",y =均值")+scale_x_continuous(limits = c(-1,4),休息= c(-1,0,1,2,3,4))+scale_y_continuous(limits = c(62,69),休息时间= c(61,62,63,64,65,66,67,68,69))+annotate("rect",xmin = -Inf,xmax = 2,ymin = -Inf,ymax = Inf,fill =浅蓝色",alpha = 0.2)+annotate("rect",xmin = 2,xmax = Inf,ymin = -Inf,ymax = Inf,填充="blue",alpha = 0.2)+theme_bw()+主题(panel.grid.minor = element_blank())
这里是显示图例的一种方法(摘自(v0.2.0)创建于2018-04-04.
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())
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).
这篇关于图例ggplot图未显示均值,错误栏和彩色矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!