r - ggplot2:标准错误色带与绘图线不匹配
问题描述
我正在尝试向我的 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))
解决方案
指定数据框和变量的子集(例如 with $
)可能会导致在 ggplot 中发生奇怪的事情。您永远不需要这样的子集,因为数据框已经在某个时候指定给data
参数(可能在第一次ggplot(...)
调用中)。与美学一样,数据也是从几何和统计继承而来ggplot
的,因此除非您更改数据,否则无需担心重新指定其来源。(如果您确实想更改使用的数据框,请指定data
相关 geom 或 stat 的参数。)
删除m$
s 将解决问题。重新排列和简化,使逻辑更明显,
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)
推荐阅读
- android - 尝试在 android studio 上运行应用程序时收到“访问被拒绝”消息
- angular - [style.width.px] 中的角度条件
- php - Yii 1.1 允许的内存大小为 536870912 字节耗尽(尝试分配 72 字节)
- node.js - aws 用 node.js 中的 aws lambda 理解
- ios - 如何处理 AVPlayer 中的超时请求失败?
- microsoft-graph-api - 从 Microsoft Teams 中的 MS Graph API 调用 Read Reports API,但使用此 Api
- typo3-extensions - Typo3 10.4.1 扩展生成器:没有为新扩展创建表
- jmeter - 使用jmeter在html响应中提取值的问题
- postgresql - 重写原始 PostgreSQL 查询以使用 TypeORM 中的 SelectQueryBuilder
- python - 如何计算 md5 哈希的所有 32 位部分中前导零的数量