简体   繁体   English

误差条在ggplot2条形图中显得太低

[英]Error Bars appearing too low in ggplot2 bar plot

Been scouring the site (and others) for a solution to the problem pictured below. 正在搜寻站点(及其他站点)以解决下图所示的问题。 I can't get the error bars to move from their 'low' position, despite trying lots of different solutions to similar problems I have encountered here and elsewhere. 尽管我尝试了许多其他解决方案来解决我在这里和其他地方遇到的类似问题,但我无法使误差线从“低”位置移开。

在此处输入图片说明

The problem occurs whether I produce one graph alone or the two and combine them with gridExtra . 发生问题是我只生成一个图还是将两个图都与gridExtra结合在一起。

Here is the code and a sample of each dataset included: 以下是其中包含的每个数据集的代码和示例:

First dataset example: 第一个数据集示例:

Group      PSQI Time_Point
1 Control    2   Baseline
2 Control    2   Baseline
3 Control    2   Baseline
4 Control   13   Baseline
5 Control    1   Baseline
6 Control    7   Baseline

Second: 第二:

  Group    ESS Time_Point
1 Control   3   Baseline
2 Control   4   Baseline
3 Control   1   Baseline
4 Control   0   Baseline
5 Control   7   Baseline
6 Control  11   Baseline

Code: 码:

library(gridExtra)
library(ggplot2)
library(grid)

p1 <- ggplot(PSQI_Graph,aes(fill=Group,y=PSQI,x=Time_Point)) +
    geom_bar(position="dodge",stat="identity", width = 0.50) +
    stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = 
position_dodge(.5), width = .25)+
    guides(fill = F)+
    scale_y_continuous(name = "Mean PSQI Score", limits=c(0,25))+
    xlab("Time Point") +
    guides(fill = guide_legend(keywidth = 1.5, keyheight = 1))+
    theme_bw()

Would really like to get this data out, so would appreciate any suggestions. 真的很想获得这些数据,因此不胜感激。

It would be better to use geom_errorbar . 最好使用geom_errorbar First, you'll have to make a new data.frame with one line per bar, including the standard error (I added an "After" timepoint to show how this looks): 首先,您必须制作一个新的data.frame ,每小节一行,其中包括标准错误(我添加了一个“ After”时间点以显示其外观):

    Group PSQI Time_Point StdErr
1 Control 4.50   Baseline   1.91
2 Control 7.17      After   0.79

Then, this ggplot call will work: 然后,此ggplot调用将起作用:

ggplot(psqi, aes(fill=Group, y=PSQI, x=Time_Point, ymin=PSQI-StdErr, ymax=PSQI+StdErr)) + #the ymin and ymax parameters here tell it where to put the top and bottom error bars
    geom_bar(stat="identity") +
    geom_errorbar() #you can change the width and thickness of the bars

Yielding this image: 产生此图像:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM