1

I have a long format dataset with 3 variables. Im plotting two of the variables and faceting by the other one, using ggplot2. I'd like to plot the standard error bars of the observations from each facet too, but I've got no idea how. Anyone knows?

Here´s a picture of what i've got. I'd like to have the standard error bars on each facet. Thanks!!

Edit: here's some example data and the plot.

data <- data.frame(rep(c("1","2","3","4","5","6","7","8","9","10",
                         "11","12","13","14","15","16","17","18","19","20",
                         "21","22","23","24","25","26","27","28","29","30",
                         "31","32"), 2),
                   rep(c("a","b","c","d","e","f","g","h","i","j","k","l"), 32),
                   rnorm(n = 384))

colnames(data) <- c("estado","sector","VA")

ggplot(data, aes(x = estado, y = VA, col = sector)) + 
  facet_grid(.~sector) + 
  geom_point()

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Welcome to StackOverflow! Please provide a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data so we have some context on how to answer your question. Otherwise, all I can recommend is the function `geom_errorbar` – Punintended Apr 27 '18 at 18:09
  • Hello. Thank you! I wasn't aware of that. Here's an example of the data, and the plot. Cheers! data<-data.frame(rep(c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32"),2),rep(c("a","b","c","d","e","f","g","h","i","j","k","l"),32),rnorm(n=384)) colnames(data)<-c("estado","sector","VA") ggplot(data,aes(x=estado,y=VA,col=sector))+facet_grid(.~sector)+geom_point() – Johan Dingler Apr 27 '18 at 18:19
  • Follow-up: Are you looking to plot mean+error-bars, or something like a boxplot, or the error bars on each individual point? – Punintended Apr 27 '18 at 18:23
  • I'd like the mean and error bars for the points in each facet :) – Johan Dingler Apr 27 '18 at 18:25
  • @JohanDingler so you looking for boxplot – denis Apr 27 '18 at 20:26

2 Answers2

2

If all you want is the mean & standard error bar associated with each "estado"-"sector" combination, you can leave ggplot to do all the work, by replacing the geom_point() line with stat_summary():

ggplot(data,
       aes(x = estado, y = VA, col = sector)) +
  facet_grid(. ~ sector) + 
  stat_summary(fun.data = mean_se)

plot

See ?mean_se from the ggplot2 package for more details on the function. The default parameter option gives you the mean as well as the range for 1 standard error above & below the mean.

If you want to show the original points, just add back the geom_point() line. (Though I think the plot would be rather cluttered for the reader, in that case...)

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
1

Maybe you could try something like below?

set.seed(1)
library(dplyr)
dat = data.frame(estado = factor(rep(1:32, 2)), 
                 sector = rep(letters[1:12], 32), 
                 VA = rnorm(384))
se = function(x) {
    sd(x)/sqrt(length(x))
}
dat_sum = dat %>% group_by(estado, sector) %>%
    summarise(mu = mean(VA), se = se(VA))
dat_plot = full_join(dat, dat_sum)
ggplot(dat_plot, aes(estado, y = VA, color = sector)) + 
    geom_jitter() + 
    geom_errorbar(aes(estado, y = mu, color = sector, 
                      ymin = mu - se, ymax = mu + se)) + 
    facet_grid(.~sector)

enter image description here

Sean Lin
  • 805
  • 4
  • 12