1

I'm working with a nested boxplot comparing observed landtypes and simulated land types. I want to show the means of each data set as a line (not a point) within the box of each boxplot.

To erase the median lines, and input the means as an "errorbar" I used the code:

 No.Forest.Plot <- ggplot(data = NoForest_Long, 
aes(x= value, y = variable, fill = ObsSim)) + 
labs(x= "Percent of Cover",  y= "Land Type Categories") + 
  ggtitle( "Observed vs. Simulated Land Categories (No Forest)") + 
 geom_boxplot(fatten = NULL) + 
stat_summary(fun = mean, fun.min = mean, 
fun.max = mean, geom = "errorbar", width = 0.5,)  

Which provides this plot. Unfortunately, the lines are centered on each category as opposed to the boxes themselves.

How do I get the means of both the "observed" and "simulated" to be centered in their respective boxes?

stefan
  • 90,330
  • 6
  • 25
  • 51
Looms
  • 13
  • 3
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 18 '21 at 17:55

1 Answers1

2

This could be achieved by setting the position of the errorbars. As geom_boxplot uses position_dodge(.75) by default you have to use the same position for your errorbars as well.

Using mtcars as example data:

library(ggplot2)

ggplot(mtcars, aes(x = mpg, y = factor(cyl), fill = factor(am))) +
  geom_boxplot(fatten = NULL) + 
  stat_summary(fun = mean, fun.min = mean, 
               fun.max = mean, geom = "errorbar", position = position_dodge(.75), width = .7)

stefan
  • 90,330
  • 6
  • 25
  • 51