ggplot2:标准错误功能区与绘图线不匹配 [英] ggplot2: standard error ribbons not matching the plot lines

查看:31
本文介绍了ggplot2:标准错误功能区与绘图线不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在ggplot中添加错误栏功能区,但是由于某些原因,阴影与绘制的线条不匹配.这是我第二次遇到此问题,我无法弄清为什么会发生这种情况.最初,我认为这可能是由于某些变量被定义为因素的方式而发生的,但是即使更改其格式,我仍然会遇到此问题.任何想法或建议将不胜感激!谢谢!

I'm trying to add error bar ribbons to my ggplot, but for some reason the shading doesn't match the plotted lines. This is the second time I encounter this problem and I cannot figure out for the life of me why this happens. Initially, I thought this may happen due to the way some variables are defined as factors, but I still get this problem even if I change their formatting. Any ideas or suggestions would be much appreciated! Thank you!

数据框:

    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))

推荐答案

指定数据帧并将子集设置为变量(例如,使用 $ )会导致ggplot中发生奇怪的事情.您永远不需要这样的子集,因为已经在某个时刻将数据帧指定给了 data 参数(可能在第一个 ggplot(...)调用中).数据就像美学一样,也从 ggplot 继承到了几何和统计信息,因此,除非您要更改数据,否则不必担心重新指定其来源.(如果要做要更改使用的数据框,请指定相关geom或stat的 data 参数.)

Specifying the data frame and subsetting to a variable (e.g. with $) can cause weird things to happen in ggplot. You never need such subsetting, as the data frame is already specified to a data parameter at some point (likely in the first ggplot(...) call). Data, like aesthetics, is inherited from ggplot to geoms and stats, too, so unless you're changing the data, there is no reason to worry about re-specifying its origin. (If you do want to change the data frame used, specify the data parameter of the relevant geom or stat.)

删除 m $ s将解决此问题.重新整理和简化,使逻辑更加明显

Dropping the m$s will fix the problem. Rearranging and simplifying so the logic is more obvious,

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)

这篇关于ggplot2:标准错误功能区与绘图线不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆