YYM17
YYM17

Reputation: 353

R ggplot2 to plot bars for group mean

I'm using the dataset from the package "gapminder" which has a variable "continent" and a variable "lifeExp" (life expectancy). I used -mutate- function to calculate the mean and sd of lifeExp for each continent and add this variable into the dataset (so each observation will have a mean of lifeExp), it will be like this: 1 When I want to use -ggplot2- package to plot bars of means of "lifeExp" (variable "mean") by "continent", it seems that R will sum up the value of "mean" like this: 2

My code is:

gap2007 %>%
  ggplot(., aes(x=continent, y=mean, fill=continent))+
  geom_bar(stat='identity')+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd))+
  xlab("Continent") + ylab("Mean life expectancy") +
  labs(title="Barplot of Average Life Expectancy with Standard Deviations")

How to plot in group level of mean not summing up the mean? Thank you!

Upvotes: 3

Views: 635

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6759

It appears that you calculated the means of lifeExp by country, then you plotted those values by continent. The easiest solution is to get the data right before ggplot, by calculating mean and sd values by continent:

library(tidyverse)
library(gapminder)

df<-gapminder %>% 
  group_by(continent) %>% 
  summarize(
    mean = mean(lifeExp), 
    median = median(lifeExp), 
    sd = sd(lifeExp)
  ) 

df %>%
  ggplot(., aes(x=continent, y=mean, fill=continent))+
  geom_bar(stat = "identity")+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd))+
  xlab("Continent") + ylab("Mean life expectancy") +
  labs(title="Barplot of Average Life Expectancy with Standard Deviations")

Created on 2020-01-16 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions