Error bar question

50 views
Skip to first unread message

Andreas

unread,
Apr 8, 2009, 8:30:17 PM4/8/09
to ggplot2
Hello folks,

I'm a long time programmer, and an academic, that dabbles in
statistical analysis for many of my experiments. I've been wanting to
replace SPSS and sigma plot with R for some time, and just finished
reading R for SAS and SPSS users, which was fantastic. But, I just
finished some very easy statistical analysis, and I'm trying to
generate some error bar graphs that match my existing publications to
make sure I'm doing everything right. These graphics are "basically"
working, but I'm not quite grasping how to get them to look how I
want.

Specifically, I have an error bar graph that is generated using the
following code:

#Error bars for Atotal
ggplot(math,
aes( as.numeric(Group), Atotal) ) +
stat_summary(fun.y="mean",
geom="smooth", se=FALSE) +
stat_summary(fun.data="mean_cl_normal",
geom="errorbar", width=.2) +
scale_x_discrete(limits=c("No Pauses", "Pauses"), name="Participant
Group") +
scale_y_continuous(limits = c(0,200), name = "Total Rating")
ggsave(file = "math/Atotal.eps")

This generally works, but has a few things I would like to change.

1. As far as I can tell, the error bars are representing standard
deviation, but I would prefer (for this particular publication) if
they presented standard error of the mean instead. My assumption was
that by changing se=FALSE to se=TRUE, that this would change the
stat_summary function to mean standard error, but when I generate the
graph, it doesn't appear to visually change, and I don't see "se"
mentioned in the documentation anywhere at:

http://had.co.nz/ggplot2/stat_summary.html

2. I would "prefer" to remove the grey background and replace it with
a white one with a black border around the entire graphic.

3. as.numeric(Group) refers to two discrete groups. The mean of each
group is connected via a bright blue line. Many publications do not
allow color. Any way to make this line black instead of blue?

4. When I call scale_y_continuous(limits = c(0,200), name = "Total
Rating"), my assumption was that it would scale the graph between
those values. It does do this, sort of, but it leaves a small buffer
above 200 and below zero. Any way to remove this?

Maybe these are dumb questions (I'm still an R beginner, after all),
so I apologize in advance, but hopefully someone out there has some
ideas.

Thanks in advance,

Andreas

hadley wickham

unread,
Apr 10, 2009, 9:32:46 AM4/10/09
to Andreas, ggplot2

That method uses smean.cl.normal from the Hmisc package and if you
read the documentation for that you'll see that it is displaying the
"standard" error.

> 2. I would "prefer" to remove the grey background and replace it with
> a white one with a black border around the entire graphic.

theme_set(theme_bw())

> 3. as.numeric(Group) refers to two discrete groups. The mean of each
> group is connected via a bright blue line. Many publications do not
> allow color. Any way to make this line black instead of blue?

stat_summary(fun.y="mean",
geom="smooth", se=FALSE, colour = "black")

> 4. When I call scale_y_continuous(limits = c(0,200), name = "Total
> Rating"), my assumption was that it would scale the graph between
> those values. It does do this, sort of, but it leaves a small buffer
> above 200 and below zero. Any way to remove this?

It's usually a good idea to leave this buffer in, but if you really
want to get rid of it, do this:

+ scale_y_continuous("Total rating", limits = c(0, 200), expand = c(0, 0))

Regards,

Hadley

--
http://had.co.nz/

Andreas Stefik

unread,
Apr 10, 2009, 12:06:35 PM4/10/09
to hadley wickham, ggplot2
Hadley,

Thanks so much for your response; this helps quite a bit.

That method uses smean.cl.normal from the Hmisc package and if you
read the documentation for that you'll see that it is displaying the
"standard" error.

The documentation does say that, but my graph is definitely displaying the standard deviation, not the standard error. I can post a picture if it helps, but I'm entirely positive that this is the case.

 

> 2. I would "prefer" to remove the grey background and replace it with
> a white one with a black border around the entire graphic.

theme_set(theme_bw())

This works, but when I re-render the image, it moves the label for the y-axis to the bottom of the graphic (It says "otal rating" on the bottom of the screen now). I've tested and it looks like this is being caused by the theme call, as it has the same behavior if I remove the expand call and the colour change. Any idea why this would move the label only on the Y-axis?
 
The rest worked great, thanks!

Andreas

hadley wickham

unread,
Apr 11, 2009, 12:34:58 AM4/11/09
to Andreas Stefik, ggplot2
Hi Andreas,

>> That method uses smean.cl.normal from the Hmisc package and if you
>> read the documentation for that you'll see that it is displaying the
>> "standard" error.
>
> The documentation does say that, but my graph is definitely displaying the
> standard deviation, not the standard error. I can post a picture if it
> helps, but I'm entirely positive that this is the case.

Yes, it would be really helpful.

>> > 2. I would "prefer" to remove the grey background and replace it with
>> > a white one with a black border around the entire graphic.
>>
>> theme_set(theme_bw())
>
> This works, but when I re-render the image, it moves the label for the
> y-axis to the bottom of the graphic (It says "otal rating" on the bottom of
> the screen now). I've tested and it looks like this is being caused by the
> theme call, as it has the same behavior if I remove the expand call and the
> colour change. Any idea why this would move the label only on the Y-axis?

Yes, that's a bug in the current version. Try

theme_set(theme_bw())
theme_update(axis.title.y =
theme_text(size = 12, angle = 90, vjust = 0.5))


Hadley

--
http://had.co.nz/

Andreas Stefik

unread,
Apr 11, 2009, 12:31:40 PM4/11/09
to hadley wickham, ggplot2
Hadley,

Thanks again, I'm getting much closer now (The theme adjustment worked), and this is really helping. I've attached a picture of the graphic in EPS form. So, in my graph there are two groups posted on the x-axis, pauses and no pauses. The no pauses group has a mean of 129 or so and a std deviation of 21.9. Since n was 8 for my study, this puts the std. error of the mean at 21.9 / the square root of 8, approximately 7.74. (SPSS and R both confirm my numbers, so I'm pretty sure there's no typos here).

However, in my graph, the error bars are clearly extending through about 20, as no pauses extends to almost as high as 150 and almost as low as 110. The argument for the pauses group has different numbers, but is otherwise similar.

Here is the code where I'm at right now:

theme_set(theme_bw())
theme_update(axis.title.y =
  theme_text(size = 12, angle = 90, vjust = 0.5)) ##This works great


ggplot(math,
  aes( as.numeric(Group), Atotal) ) +
  stat_summary(fun.y="mean",
    geom="point", colour="black") +
  stat_summary(fun.y="mean",
    geom="smooth", colour="black") +
  stat_summary(fun.data="mean_cl_normal",
    geom="errorbar", se=FALSE, width=.2) + ##Or true, doesn't seem to matter

  scale_x_discrete(limits=c("No Pauses", "Pauses"), name="Participant Group") +
  scale_y_continuous(name ="Total Rating", limits = c(0,200), expand=c(0,0))

Hopefully that helps; ideas are definitely welcome.

Andreas

P.S. I've attached the file in both EPS and PDF format, just in case you don't happen to have an EPS reader loaded. Same graphic either way though.
Atotal.eps
Atotal.pdf

hadley wickham

unread,
Apr 11, 2009, 12:51:59 PM4/11/09
to Andreas Stefik, ggplot2
On Sat, Apr 11, 2009 at 11:31 AM, Andreas Stefik <ste...@gmail.com> wrote:
> Hadley,
>
> Thanks again, I'm getting much closer now (The theme adjustment worked), and
> this is really helping. I've attached a picture of the graphic in EPS form.
> So, in my graph there are two groups posted on the x-axis, pauses and no
> pauses. The no pauses group has a mean of 129 or so and a std deviation of
> 21.9. Since n was 8 for my study, this puts the std. error of the mean at
> 21.9 / the square root of 8, approximately 7.74. (SPSS and R both confirm my
> numbers, so I'm pretty sure there's no typos here).
>
> However, in my graph, the error bars are clearly extending through about 20,
> as no pauses extends to almost as high as 150 and almost as low as 110. The
> argument for the pauses group has different numbers, but is otherwise
> similar.

By default you get 95% confidence intervals - i.e. 7.74 * 1.96 which
seems about what you get on the graph. It would also be helpful if
you could include your data so I can double-check.

Hadley

--
http://had.co.nz/

Andreas Stefik

unread,
Apr 14, 2009, 2:03:18 PM4/14/09
to ggp...@googlegroups.com
Hello folks,

I'm close to having my error bar graphs how I want them. My code now looks as so:




theme_set(theme_bw())
theme_update(axis.title.y =
  theme_text(size = 12, angle = 90, vjust = 0.5))

#Error bars for Atotal

ggplot(math,
  aes( as.numeric(Group), Atotal) ) +
  stat_summary(fun.y="mean",
    geom="point", colour="black") +
  stat_summary(fun.y="mean",
    geom="smooth", colour="black") +
  stat_summary(fun.data="mean_cl_normal", conf.int=0.68,
    geom="errorbar", se=FALSE, width=.2) + ##Or true
  scale_x_discrete(limits=c("No Pauses", "Pauses"), name="Participant Group") +
  scale_y_continuous(name ="Total Rating", limits = c(0,200), expand=c(0,0))
ggsave(file = "math/Atotal.eps")

The suggestion seems to be to change the confidence intervals using conf.int to lower my errors bars to indicate standard error instead of 95% confidence intervals, but as far as I can tell, this doesn't actually change my graphs at all. Is this a bug?

Andreas

hadley wickham

unread,
Apr 16, 2009, 9:18:22 AM4/16/09
to Andreas Stefik, ggp...@googlegroups.com
>>
>> theme_set(theme_bw())
>> theme_update(axis.title.y =
>>   theme_text(size = 12, angle = 90, vjust = 0.5))
>>
>> #Error bars for Atotal
>> ggplot(math,
>>   aes( as.numeric(Group), Atotal) ) +
>>   stat_summary(fun.y="mean",
>>     geom="point", colour="black") +
>>   stat_summary(fun.y="mean",
>>     geom="smooth", colour="black") +
>>   stat_summary(fun.data="mean_cl_normal", conf.int=0.68,
>>     geom="errorbar", se=FALSE, width=.2) + ##Or true
>>   scale_x_discrete(limits=c("No Pauses", "Pauses"), name="Participant
>> Group") +
>>   scale_y_continuous(name ="Total Rating", limits = c(0,200),
>> expand=c(0,0))
>> ggsave(file = "math/Atotal.eps")
>
> The suggestion seems to be to change the confidence intervals using conf.int
> to lower my errors bars to indicate standard error instead of 95% confidence
> intervals, but as far as I can tell, this doesn't actually change my graphs
> at all. Is this a bug?

Yes, it is. I'll look into it.

Hadley


--
http://had.co.nz/

hadley wickham

unread,
Apr 16, 2009, 11:55:05 PM4/16/09
to Andreas Stefik, ggp...@googlegroups.com
>> The suggestion seems to be to change the confidence intervals using conf.int
>> to lower my errors bars to indicate standard error instead of 95% confidence
>> intervals, but as far as I can tell, this doesn't actually change my graphs
>> at all. Is this a bug?
>
> Yes, it is.  I'll look into it.

Ok, will be fixed in the next version (which I'm aiming to release
tomorrow). You can also use mult = 1.

Hadley

--
http://had.co.nz/

Andreas Stefik

unread,
Apr 21, 2009, 11:58:28 AM4/21/09
to hadley wickham, ggp...@googlegroups.com
Hadley,

I've downloaded the new version, and as far as I can tell, the patch seemed to work. I added mult=1 to my code and the error bars shrunk to, so far as I can tell visually, the standard error.

Thanks again for your help, it looks as if that did the trick.

Andreas

P.S. If I can ask, though, is there any documentation somewhere on mult=1 or conf.int. It would be nice to double check the actual values it's giving just to be sure it's correct.

hadley wickham

unread,
Apr 21, 2009, 12:01:17 PM4/21/09
to Andreas Stefik, ggp...@googlegroups.com
> P.S. If I can ask, though, is there any documentation somewhere on mult=1 or
> conf.int. It would be nice to double check the actual values it's giving
> just to be sure it's correct.

Check out ?smean.cl.normal

Hadley


--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages