RE: alignment of error bar on bar chart

3,412 views
Skip to first unread message

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 16, 2012, 1:50:37 PM9/16/12
to sathish, ggp...@googlegroups.com
Hello.

I have been experiencing similar problems with the ggplot2 bar plot: the error bar moves away from the geom_bar. The error bar does not stay on the center of the geom_bar. I would appreciate receiving any advice one could offer to resolve the issue. My code is shown below:

library(ggplot2)
dodge <- position_dodge (width=1)
p <- ggplot(data = tempdata,
aes(x = age, y = est, fill = spd)) +
geom_bar(position=dodge, width=0.50) +
geom_errorbar( aes(ymax = est+se, ymin = est), width=.25, position =dodge)+
scale_fill_manual(values=c("#996633","black"))+
xlab("")+
ylab("Percentage - Current Smokers") +
labs (spd=" ")+


opts(title=" ",
axis.title.y = theme_text(face = 'bold', size = 25, angle = 90),
axis.text.y=theme_text(size=25, hjust=1, face="bold"),
axis.text.x=theme_text(angle=90, hjust=1, size=25, face="bold"),
legend.text = theme_text(size= 20,hjust=1, face="bold"),
legend.title=theme_blank (),
#panel.background=theme_rect(),
plot.title = theme_text(size=24, face="bold"),
plot.title =" "
)

print (p)
ggsave(file='curr_smk_spd.png', width=11, height=7)



________________________________________
From: ggp...@googlegroups.com [ggp...@googlegroups.com] On Behalf Of sathish [sathis...@gmail.com]
Sent: Friday, September 07, 2012 1:28 PM
To: ggp...@googlegroups.com
Subject: alignment of error bar on bar chart

Hi there,
I am getting the error bar shifted towards the corner of the bars in bar chart, but I need error bars automagically placed at the center of each bar when the width of the bar is manually changed to different levels. When I changed the width of the error bar equal to the width of the bar, then the error bar moves to the center, otherwise it stays away from the center. The aesthetics of the bar chart would be much better if the error bar stays at the center and that error bar has width less than the width of the bar itself. I would appreciate any suggestion on this problem.

Thanks, Sathish

package ‘ggplot2’ version 0.9.1

df <- data.frame(trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
se = c(0.1, 0.3, 0.3, 0.2))

ggplot(data = df,
aes(x = trt,
y = resp,
fill = group)) +
geom_bar(position = "dodge",
width = 0.5) +
geom_errorbar(aes(ymax = resp + se,
ymin = resp - se),
position = "dodge",
width = 0.25)




--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Srinivasan, Sathish K

unread,
Sep 16, 2012, 5:32:39 PM9/16/12
to Muhuri, Pradip (SAMHSA/CBHSQ), sathish, ggp...@googlegroups.com, Jonas Kuppler
Hi there,

Thanks Jonas for your reply.

The solution for this problem is to remove the attribute "width = 0.5" in line 5. The bars will dodge at width =1 specified in line 2. The value 0.25 for width of error bars will be applied as in line 6. Make sure you don't double quote the attribute dodge throughout your code, otherwise it will assume the default dodge value that is 0.9.

This problem may have to do something with inheritance while creating instances of an object.

The upshot is run your code as it is and after deleting width=0.5 in line 5. In other words, line 5 should be ...geom_bar(position=dodge)... but not ...geom_bar(position=dodge, width=0.50)... If you want to tweak the width of bar dodging and error bars, you have adjust the value of width in line 2 for bar dodging and adjust the value of width in line 6 for error bar.

Sathish
________________________________________
From: ggp...@googlegroups.com [ggp...@googlegroups.com] On Behalf Of Muhuri, Pradip (SAMHSA/CBHSQ) [Pradip...@samhsa.hhs.gov]
Sent: Sunday, September 16, 2012 1:50 PM
To: sathish; ggp...@googlegroups.com
Subject: RE: alignment of error bar on bar chart

Srinivasan, Sathish K

unread,
Sep 16, 2012, 6:12:25 PM9/16/12
to Muhuri, Pradip (SAMHSA/CBHSQ), sathish, ggp...@googlegroups.com, Jonas Kuppler
Hi,
In my previous message, I explained adjusting the value of width in line 2 would alter the width of "bar dodging". But, it would not assist in controlling the width of the "bar" itself. The solution from Jonas gives a fine control over the width of the bar and the error bar.

Here is the modified code:

library(ggplot2)
p <- ggplot(data = tempdata,
aes(x = age, y = est, fill = spd)) +
geom_bar(position="dodge", width=0.50) +
geom_errorbar( aes(ymax = est+se, ymin = est),
position = position_dodge(width=0.5), width=0.25)+
...
To tweak the width of the bar, adjust the value in line 4. Notice the double quote in geom_bar(position="dodge", width...) in line 4. If you remove double quote, you will see overlapping of dodged bars which is not what I expect in my bar chart. Probably someone can explain the significance of double quotes specifically to this problem and in general for all ggplot2 graphs.

For error bar, there are two values specified in line 6. One is for the placing the error bar at a position having half of the specified value in position_dodge(width=0.5), so this value can be same as the value in line 4 in order for the error bar be placed at the center, and the second value is for adjusting the width of the error bar: width = 0.25.

Sathish
________________________________________
From: Srinivasan, Sathish K
Sent: Sunday, September 16, 2012 5:32 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ); sathish; ggp...@googlegroups.com
Cc: Jonas Kuppler

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 17, 2012, 10:26:05 AM9/17/12
to Srinivasan, Sathish K, sathish, ggp...@googlegroups.com, Jonas Kuppler
Hi,

Thank you so much for sending me the modified code and providing detailed explanation of the code.

Pradip

Pradip K. Muhuri, PhD
Statistician
Substance Abuse & Mental Health Services Administration
The Center for Behavioral Health Statistics and Quality
Division of Population Surveys
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
 
Tel: 240-276-1070
Fax: 240-276-1260
e-mail: Pradip...@samhsa.hhs.gov
 
The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:  http://cbhsqsurvey.samhsa.gov

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 17, 2012, 10:27:50 AM9/17/12
to Jonas Kuppler, ggp...@googlegroups.com, sathish

Hi,

 

Thank you so much for providing the correct code, which has worked.

 

Pradip

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Jonas Kuppler
Sent: Sunday, September 16, 2012 3:07 PM
To: ggp...@googlegroups.com
Cc: sathish
Subject: Re: alignment of error bar on bar chart

 

Hi,

You have to specify the position for the errorbars yourself. Following code should work:



df <- data.frame(trt = factor(c(1, 1, 2, 2)),
                 resp = c(1, 5, 3, 4),
                 group = factor(c(1, 2, 1, 2)),
                 se = c(0.1, 0.3, 0.3, 0.2))

ggplot(data = df,
aes(x = trt, y = resp,fill = group)) +

geom_bar(position="dodge",width = 0.5) +
geom_errorbar(aes(ymax = resp + se,ymin = resp - se), position=position_dodge(width=0.5), width=0.25)

HTH,

Jonas

Reply all
Reply to author
Forward
0 new messages