runningbirds
runningbirds

Reputation: 6615

Plotting Average/Median of each column in data frame grouped by factors

I am trying to make a grouped barplot and I am running into trouble. For example, if I was using the mtcars dataset and I wanted to group everything by the 'vs' column (col #8), find the average of all remaining columns, and then plot them by group.

Below is a very poor example of what I am trying to do and I know it is incorrect.

Ideally, mpg for vs=1 & vs=0 would be side by side, followed by cyl's means side by side, etc. I don't care if aggregate is skipped for dyplr or if ggplot is used or even if the aggregate step is not needed...just looking for a way to do this since it is driving me crazy.

df = mtcars
agg = aggregate(df[,-8], by=list(df$vs), FUN=mean)
agg
barplot(t(agg), beside=TRUE, col=df$vs))

Upvotes: 0

Views: 4272

Answers (1)

akrun
akrun

Reputation: 887223

Try

library(ggplot2)
library(dplyr)
library(tidyr)
df %>% 
    group_by(vs=factor(vs)) %>%
    summarise_each(funs(mean)) %>% 
    gather(Var, Val, -vs) %>%
    ggplot(., aes(x=Var, y=Val, fill=vs))+
    geom_bar(stat='identity', position='dodge')

enter image description here

Or using base R

 m1 <- as.matrix(agg[-1])
 row.names(m1) <- agg[,1]
 barplot(m1, beside=TRUE, col=c('red', 'blue'), legend=row.names(m1))

enter image description here

Upvotes: 2

Related Questions