Skip to content

Instantly share code, notes, and snippets.

@1beb
Created June 19, 2011 20:50
Show Gist options
  • Save 1beb/1034736 to your computer and use it in GitHub Desktop.
Save 1beb/1034736 to your computer and use it in GitHub Desktop.
PartII ggplot2 Introduction
## Title: ggplot2 Introduction: Part 2
## Description: This line by line analysis, provides an introduction to ggplot2. How to deal with factors, subsetting and labels
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc.
# install.packages("ggplot2")
## Load Library
library(ggplot2)
# load sample data
d <- diamonds
# Start with a boxplot of cut and price.
ggplot(d, aes(cut, price)) + geom_boxplot()
# Look at mean price by cut (I prefer plyr, but aggregate(), and by() provide the same functionality)
d <- ddply(d, .(cut), function(x) data.frame(
x,
mean=mean(x$price)
))
# Show it
head(d)
tail(d)
# Unordered tends to look a bit messy.
ggplot(d, aes(cut, mean)) + geom_bar()
# Use reorder() to provide sanity
ggplot(d, aes(reorder(cut,mean), mean)) + geom_bar()
# Or transform factors before hand, this makes calling ggplot a little easier to read
# especially when you have a lot of layers
d <- transform(d, cut=reorder(cut, mean))
ggplot(d, aes(cut, mean)) + geom_bar()
# Imagine you only wanted to look at the subset where mean > 4000?
d.4k <- subset(d, mean > 4000)
# Let's plot!
ggplot(d.4k, aes(cut, mean)) + geom_bar()
# Don't forget to droplevels() during, or after you subset. Otherwise, ggplot will print left over levels
ggplot(droplevels(d.4k), aes(cut, mean)) + geom_bar()
# Clients tend to like seeing the numbers though, don't they?
ggplot(d, aes(reorder(cut, mean), mean, label=mean)) + geom_bar() + geom_text()
## Can't see the numbers very well? There are a few options
# Change the colour and reduce the decimal points
ggplot(d, aes(reorder(cut, mean), mean, label=round(mean,0))) + geom_bar() + geom_text(colour="white")
# Add a vertical adjustment.
ggplot(d, aes(reorder(cut, mean), mean, label=round(mean,0))) + geom_bar() + geom_text(colour="white", vjust=2)
# or, set the y aethetic for geom_text()
ggplot(d, aes(reorder(cut, mean), mean, label=round(mean,0))) + geom_bar() + geom_text(colour="white", y=1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment