Home > database >  Using geom_pointrange() to plot means and standard errors
Using geom_pointrange() to plot means and standard errors

Time:06-09

I have three groups (categorical variable) who completed a test and a dataframe with the mean and standard error on the test per group. I would like to plot their means as a single point in the plot accompanied by a short horizontal line indicating the standard error (i.e., error bars). I'm using R with ggplot2.

My x-axis represents all the possible scores in the test (from -218 to 218) and the groups are plotted on the y-axis (I used coord_flip() for this).

I was able to create the graph but the standard error lines don't show up, so I don't know what I'm doing wrong. I think it has to do with my use of geom_pointrange(), but I have no idea what I'm supposed to change.

This is my code:

ggplot(descriptive_blp_data)  
aes(x = group, y = mean_blp, colour = group, size = 5)  
geom_pointrange(aes(ymin = mean_blp - se_blp, ymax = mean_blp   se_blp), width=.2,
             position=position_dodge(.9))  
scale_color_manual(
values = list(
  Group_2 = "#9EBCDA",
  Group_3 = "#8856A7",
  Group_1 = "#E0ECF4"
)
)  
labs(y = "Mean BLP score (SE)")  
coord_flip()  
theme_classic()  
theme(legend.position = "none", axis.title.y = element_blank())  
ylim(-218, 218)

And this is my graph so far: my_grpah

CodePudding user response:

It is easier to check this, if you can provide the actual dataframe descriptive_blp_data. Running your code with some arbitrary dataset does work as intended and produces error bars, so there is nothing really wrong with the ggplot part.

There may be a few reasons why this does not work with your actual dataset - maybe the standard errors are too small to show up with a point size of 5?

descriptive_blp_data <- data.frame(
  "group" = c("Group_3", "Group_2", "Group_1"),
  "mean_blp" = c(150, 50, -50),
  "se_blp" = c(40, 20, 30)
)

library(ggplot2)

ggplot(descriptive_blp_data)  
  aes(x = group, y = mean_blp, colour = group, size = 5)  
  geom_pointrange(aes(ymin = mean_blp - se_blp, ymax = mean_blp   se_blp), width=.2,
                  position=position_dodge(.9))  
  scale_color_manual(
    values = list(
      Group_2 = "#9EBCDA",
      Group_3 = "#8856A7",
      Group_1 = "#E0ECF4"
    )
  )  
  labs(y = "Mean BLP score (SE)")  
  coord_flip()  
  theme_classic()  
  theme(legend.position = "none", axis.title.y = element_blank())  
  ylim(-218, 218)

enter image description here

  • Related