Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: plot mean values instead of counts

I have the following sample data, which describe how long (duration) each respondent in my questionnaire needed to answer each question (question) and in which week the question was answered (there were several waves of the same questionnaire)

n <- 100

data2 <- data.frame(
  question = paste("q", sample(1:3, n, replace = TRUE), sep=""),
  week = sample(1:3, n, replace = TRUE),
  duration = round(rnorm(n, mean = 15, sd = 5), 0)
)

ggplot(aes(x = week, fill=question), data = data2) + geom_bar(position='stack') +
xlab("Week")

But how can I make the same plot with mean durations instead of counts (on the y-axis)?

like image 446
D. Studer Avatar asked May 30 '26 09:05

D. Studer


2 Answers

stat_summary is the usual function for doing the calculations for you:

  ggplot(aes(x = week, y = duration, fill=question), data = data2) +
    stat_summary(fun=mean, geom="bar", position = "stack") +
    xlab("Week")

cols from stat_summary

Edit: This changed my practice the other week when I found out what stat_ layers can do! Demystifying stat_ layers in ggplot2

like image 80
Andy Baxter Avatar answered Jun 01 '26 02:06

Andy Baxter


I would use dplyr::group_bylike so.

data2 %>% 
  group_by(week,question) %>% 
  summarise(mean = mean(duration)) %>% 
  ggplot(aes(week,mean,fill = factor(question))) +
  geom_col()

Good luck!

like image 23
Magnus Nordmo Avatar answered Jun 01 '26 00:06

Magnus Nordmo