Last active
December 18, 2015 19:39
-
-
Save Ram-N/5834486 to your computer and use it in GitHub Desktop.
Learn how to group data for ggplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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