From statmethods.net.
# ggplot2 examples
library(ggplot2)
#use color brewer as default discrete colors
scale_colour_discrete <- function(...) scale_color_brewer(palette="Set1", ...)
scale_fill_discrete <- function(...) scale_fill_brewer(palette="Set1", ...)
data('mtcars')
# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1),
labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6cyl 160 110 3.90 2.620 16.46 0 Manual 4gears 4
## Mazda RX4 Wag 21.0 6cyl 160 110 3.90 2.875 17.02 0 Manual 4gears 4
## Datsun 710 22.8 4cyl 108 93 3.85 2.320 18.61 1 Manual 4gears 1
## Hornet 4 Drive 21.4 6cyl 258 110 3.08 3.215 19.44 1 Automatic 3gears 1
## Hornet Sportabout 18.7 8cyl 360 175 3.15 3.440 17.02 0 Automatic 3gears 2
## Valiant 18.1 6cyl 225 105 2.76 3.460 20.22 1 Automatic 3gears 1
Grouped by number of gears (indicated by color).
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
main="Distribution of Gas Milage", xlab="Miles Per Gallon",
ylab="Density")
## Scatterplot of mpg vs. hp ## For each combination of gears and cylinders in each facet, transmission type is represented by shape and color.
Separate for each number of cylinders.
Observations (points) are overlayed and jittered.
qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"),
fill=gear, main="Mileage by Gear Number",
xlab="", ylab="Miles per Gallon")
Author: Jim Hester Created: 2013 Mar 20 10:57:07 AM Last Modified: 2014 Apr 17 04:53:11 PM