我正在尝试向我的ggplot添加错误条带,但由于某种原因,阴影与绘制的线条不匹配。这是我第二次遇到这个问题,我无法弄清楚为什么会发生这种情况。最初,我认为这可能是由于某些变量被定义为因素的方式,但即使我更改其格式,我仍然会遇到此问题。任何想法或建议将不胜感激!谢谢!
数据框:
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))
图表代码:
{{1}}
答案 0 :(得分:4)
指定数据框并对变量进行子集化(例如使用$
)可能会导致ggplot中发生奇怪的事情。您永远不需要这样的子集,因为数据帧已经在某个时刻指定给data
参数(可能在第一个ggplot(...)
调用中)。数据,如美学,也从ggplot
继承到geoms和stats,所以除非你改变数据,否则没有理由担心重新指定它的来源。 (如果您执行想要更改使用的数据框,请指定相关geom或stat的data
参数。)
删除m$
将解决问题。重新排列和简化使逻辑更加明显,
library(ggplot2)
m <- data.frame(sound = c("English", "English", "English", "English", "Silence", "Silence", "Silence", "Silence"),
`Reading condition` = c("normal", "normal", "trailing mask", "trailing mask", "normal", "normal", "trailing mask", "trailing mask"),
diff = c("Difficult questions", "Easy questions", "Difficult questions", "Easy questions", "Difficult questions", "Easy questions", "Difficult questions", "Easy questions"),
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),
check.names = FALSE)
ggplot(m, aes(x = sound, y = Mean, ymax = Mean + SE, ymin = Mean - SE,
color = `Reading condition`, fill = `Reading condition`,
group = `Reading condition`)) +
geom_line() +
geom_point() +
geom_ribbon(alpha = 0.15, colour = NA) +
facet_grid(. ~ diff)
答案 1 :(得分:0)
通过这种方式考虑Alistaire的正确答案:ggplot采用您的起始数据框架,并应用您的ggplot代码所需的所有变换,比例和统计数据。然后在结果坐标系中生成图形。
通过将功能区的aes锚定到具有m $ ...的全局环境data.frame,您将指定预变换,pre-stat,预缩放坐标...有时您的原始坐标系可能是容易混淆地靠近隐藏的坐标系,以便你的元素出现,但只是有点偏离,正如你所看到的。