jesusgarciab
jesusgarciab

Reputation: 129

Plot Grouped bar graph with calculated standard deviation in ggplot

I feel like this should be really easy to do, but I'm having a really hard time figuring this out.

I have a data frame

type <- c("a","b","c","d","e")
x <- rnorm(5)
y <- rnorm(5)
z <- rnorm(5) 
xsd <- sd(x)
ysd <- sd(y)
zsd <- sd(z)
df <- data.frame(type, x,y,z,xsd,ysd,zsd)
df



type           x          y         z       xsd       ysd       zsd
1    a -1.16788106  0.2260430 -1.16788106 0.8182508 0.7321015 0.9016335
2    b -0.09955193 -0.6647980 -0.09955193 0.8182508 0.7321015 0.9016335
3    c -0.87901053 -0.4269936 -0.87901053 0.8182508 0.7321015 0.9016335
4    d -0.87861339 -1.3669793 -0.87861339 0.8182508 0.7321015 0.9016335
5    e  0.84350228  0.4702580  0.84350228 0.8182508 0.7321015 0.9016335

and I need a grouped bar graph of the mean of x, y, and z by type with error bars showing the standard deviation for each variable. The standard deviation is in different columns xsd,ysdand zsd

I need to plot the mean in the y axis, type grouping the x, y, z variables in the x axis. I tried using gather(), to rearrange the data, but I'm not having any success...

Upvotes: 2

Views: 2488

Answers (2)

Jack Brookes
Jack Brookes

Reputation: 3830

Let ggplot2 do the calculations for you:

install.packages("hmisc") # for mean_sdl

library(tidyverse)

type <- c("a","b","c","d","e")
x <- rnorm(5, 10, 5)
y <- rnorm(5, 8, 3)
z <- rnorm(5, 2, 4) 

df <- data.frame(type,x,y,z)

df_long <- df %>% 
  gather(variable, value, x:z)

ggplot(df_long, aes(x = variable, y = value, fill = variable)) + 
  stat_summary(fun.y = "mean", geom = "col") + 
  stat_summary(fun.data = mean_sdl, geom = "errorbar", width = .5, fun.args = list(mult = 1))

enter image description here

Upvotes: 2

AntoniosK
AntoniosK

Reputation: 16121

This example should help:

type <- c("a","b","c","d","e")
x <- rnorm(50,20, 5)
y <- rnorm(50, 25,1)
z <- rnorm(50, 40, 1) 

df <- data.frame(type, x,y,z)
df

library(tidyverse)

df %>%
  gather(x,value,-type) %>%
  group_by(type, x) %>%
  summarise(MEAN = mean(value),
            SD = sd(value)) %>%
  ggplot(aes(x, MEAN, fill=type))+
  geom_bar(stat="identity", position = "dodge")+
  geom_errorbar(aes(ymin=MEAN-SD, ymax=MEAN+SD), position = "dodge")

Upvotes: 1

Related Questions