0

I'm trying to get means for a quantitative variable, then plot means for this quant variable BY day of week (categorical)

Have tried reorganizing data and such, but to now avail.

Very simple but has me stumped. Thanks!

Jason Bu
  • 11
  • 2
  • `barplot(tapply(rnorm(100), rep_len(1:3, 100), mean))` ? fyi histograms/barplots should not be used for continuous data – rawr Mar 18 '17 at 23:02

2 Answers2

1

general example: average income by day

library(data.table)
library(ggplot2)

setDT(data)

# get average income by day
  temp <- data[, .(mean_income= mean(income, na.rm=T)), by = day]

# plot
  ggplot(data=temp) + 
    geom_bar( aes(x=day, y= mean_income) , stat = "identity") +
    labs(x="day of the week", y="average income")
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
0

how about:

library(dplyr)
x <- data.frame(day = rep(c("monday", "tuesday",  "wednesday") ,3), val = runif(9))

x2 <- x %>%
group_by(day) %>% 
summarise(mean = mean(val))

barplot(x2$mean, names.arg = x2$day)

enter image description here

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29