简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM