【发布时间】:2018-05-08 20:26:04
【问题描述】:
我正在尝试向我的 ggplot 添加错误栏色带,但由于某种原因,阴影与绘制的线不匹配。这是我第二次遇到这个问题,我一生都无法弄清楚为什么会发生这种情况。最初,我认为这可能是由于某些变量被定义为因素的方式,但即使我更改了它们的格式,我仍然会遇到这个问题。任何想法或建议将不胜感激!谢谢!
数据框:
m<- structure(list(sound = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L), .Label = c("Silence", "English"), class = "factor"), `Reading condition` = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("normal", "trailing mask"
), class = "factor"), diff = structure(c(2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L), .Label = c("Easy questions", "Difficult questions"
), class = "factor"), Mean = c(0.283, 0.36, 0.183, 0.227, 0.19,
0.347, 0.197, 0.333), SD = c(0.500558971667007, 0.34814947033031,
0.497195715406262, 0.447163568809774, 0.49804988833968, 0.361515576848703,
0.498812908487118, 0.373926502247132), SE = c(0.100111794333401,
0.0696298940660619, 0.0994391430812524, 0.0894327137619548, 0.099609977667936,
0.0723031153697406, 0.0997625816974236, 0.0747853004494263)), .Names = c("sound",
"Reading condition", "diff", "Mean", "SD", "SE"), row.names = c(NA,
8L), idvars = c("sound", "mask", "diff"), rdimnames = list(structure(list(
sound = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("English",
"Silence"), class = "factor"), mask = structure(c(1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
diff = c("difficult", "easy", "difficult", "easy", "difficult",
"easy", "difficult", "easy")), .Names = c("sound", "mask",
"diff"), row.names = c("English_No_difficult", "English_No_easy",
"English_Yes_difficult", "English_Yes_easy", "Silence_No_difficult",
"Silence_No_easy", "Silence_Yes_difficult", "Silence_Yes_easy"
), class = "data.frame"), structure(list(variable = structure(c(1L,
1L), .Label = "accuracy", class = "factor"), result_variable = structure(1:2, .Label = c("M",
"SD"), class = "factor")), .Names = c("variable", "result_variable"
), row.names = c("accuracy_M", "accuracy_SD"), class = "data.frame")), class = "data.frame")
图形代码:
library(ggplot2)
limits <- aes(ymax = m$Mean + m$SE, ymin=m$Mean - m$SE)
Dplot<- ggplot(data= m, aes(x=sound, y= Mean, color= `Reading condition`,
fill= `Reading condition`,
group=`Reading condition`, shape=`Reading condition`,
linetype=`Reading condition`))+
scale_fill_brewer(palette="Dark2")+ scale_colour_brewer(palette="Dark2")+
theme_bw() + theme(panel.grid.major = element_line(colour = "#E3E5E6", size=0.7),
axis.line = element_line(colour = "black", size=1),
panel.border = element_rect(colour = "black", size=1, fill = NA))+
geom_line(size=2) + ylim(0, 0.5)+
geom_point(size=7)+ scale_x_discrete(expand=c(0.2,0.2))+
#scale_y_continuous(limits- c(0,0.50))+
xlab("\n Background sound")+ ylab("Comprehension above chance level (%)")+
theme(legend.position="bottom", legend.title=element_text(size=20, face="bold", family="serif"), legend.text=element_text(size=20,family="serif"),legend.key.width=unit(2,"cm"),
legend.key.height=unit(1,"cm"), strip.text=element_text(size=20, family="serif"),
title=element_text(size=20, family="serif"),
axis.title.x = element_text(size=20, face="bold", family="serif"), axis.title.y = element_text(size=20, face="bold", family="serif"),
axis.text=element_text(size=20, family="serif"),
panel.border = element_rect(linetype = "solid", colour = "black"),
legend.key = element_rect(colour = "#000000", size=1))+
geom_ribbon(limits, alpha=0.15, colour=NA) +
facet_grid(.~ diff) + theme(strip.text.x = element_text(size = 20, face="bold",family="serif"),
strip.background = element_rect(fill="#F5F7F7", colour="black", size=1.5),
legend.key = element_rect(colour = "#000000", size=1))
【问题讨论】:
-
不要在
aes中使用$子集。如果你放弃它,一切都会正常运行。