Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active December 18, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ram-N/5834486 to your computer and use it in GitHub Desktop.
Save Ram-N/5834486 to your computer and use it in GitHub Desktop.
Learn how to group data for ggplot
library(ggplot2)
unique(diamonds$cut)
unique(diamonds$color)
names(diamonds)
h <- ggplot(diamonds[1:1000,], aes(x=cut)) + geom_bar(); h #simple histogram
h <- ggplot(diamonds[1:1000,], aes(x=cut, fill=color)) + geom_bar(); h # stacked by default
h <- ggplot(diamonds[1:1000,], aes(x=cut, fill=color)) + geom_bar(position="dodge"); h #unstack using dodge
h
#add a new column to the data.frame
iris2 <- ddply(iris, "Species", transform, avg.petal = mean(Petal.Length))
ggplot(iris2) + geom_boxplot(aes(x=Species, y=avg.petal))
ggplot(iris2) + geom_bar(aes(x=Species, y=avg.petal))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment