Reputation: 7948
I have some aggregate data that I would like to visualize using ggplot2 and I am now looking for a smart shortcut to plot aggregate data?
The data I have looks like this, (I currently only have data like df2
)
df <- data.frame(cyl = mtcars[,2])
(df2 <- t(table(df$cyl)))
4 6 8
[1,] 11 7 14
and I am interested to create a plot like this, (see below for code
used to create this plot)
require(ggplot2)
df <- data.frame(cyl = mtcars[,2])
df$cyl2 <- ifelse(df$cyl > 4, c('Treatment'), c('Control'))
# hline.data <- data.frame(cyl2 = c('Treatment', 'Control'), cyl = c(10, 2))
c <- ggplot(df, aes(factor(cyl)))
c + geom_bar(fill="white", colour="darkgreen", alpha=0.5) + facet_grid(. ~ cyl2, scales = "free", space = "free") + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks.x = element_blank(), legend.position = "none") + scale_y_continuous(breaks=c(4, 6, 8), labels=c("Minimal","Mild", "Moderate")) + scale_x_discrete(breaks=c(6,8,4), labels=c("Treat 1", "Treat 2", "Control"))
Bonus question for people who make it this far. Is it possible to make the breaks=
in the scale_y_continuous
different in the two facets? Or would it be a better strategy to add an geom_hline
and add some text in the margins? It seems as it is quite tricky to add in margins, I would have to override the clipping like this or and this?
Upvotes: 2
Views: 3962
Reputation: 60180
You basically just need to get your aggregate data into a dataframe, then add in the group labels:
df <- data.frame(cyl = mtcars[,2])
(df2 <- t(table(df$cyl)))
# Assume you know the group labels
df2 <- data.frame(val=df2[1, ], label=c("Control", "Treat1", "Treat2"))
df2$cyl2 <- c("Control", "Treatment", "Treatment")
ggplot(df2, aes(label, val)) +
geom_bar(fill="white", colour="darkgreen", alpha=0.5, stat="identity") +
facet_grid(. ~ cyl2, scales = "free", space = "free") +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.ticks.x = element_blank(), legend.position = "none") +
scale_y_continuous(breaks=c(4, 6, 8), labels=c("Minimal","Mild", "Moderate")) +
scale_x_discrete(breaks=c(6,8,4), labels=c("Treat 1", "Treat 2", "Control"))
Upvotes: 1