1 Basic Plot Types

library(ggplot2)

df = data.frame(x = c(3, 1, 5), y = c(2, 4, 6), label = c("a", "b", "c"))

p <- ggplot(data = df, mapping = aes(x = x, y = y, label = label)) + xlab(label = NULL) + ylab(label = NULL)

p + geom_point() + ggtitle(label = "geom_point")
p + geom_bar(stat = "identity") + ggtitle(label = "geom_bar(stat = 'identity')")
p + geom_line() + ggtitle(label = "geom_line")
p + geom_area() + ggtitle(label = "geom_area")
p + geom_path() + ggtitle(label = "geom_path")
p + geom_text() + ggtitle(label = "geom_text")
p + geom_tile() + ggtitle(label = "geom_tile")
p + geom_polygon() + ggtitle(label = "geom_polygon")

2 Displaying Distributions

library(ggplot2)

data("diamonds")

depth_distr <- ggplot(data = diamonds, mapping = aes(x = depth)) + xlim(c(55, 70))

depth_distr + geom_histogram()
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
depth_distr + geom_histogram(mapping = aes(y = ..density..), binwidth = 0.1) + facet_grid(facets = cut ~ .)
## Warning: position_stack requires constant width: output may be incorrect
## Warning: position_stack requires constant width: output may be incorrect
## Warning: position_stack requires constant width: output may be incorrect
## Warning: position_stack requires constant width: output may be incorrect
## Warning: position_stack requires constant width: output may be incorrect
depth_distr + geom_histogram(mapping = aes(fill = cut), binwidth = 0.1, position = "fill")
## Warning: position_fill requires constant width: output may be incorrect
depth_distr + geom_freqpoly()
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
## Warning: Removed 2 rows containing missing values (geom_path).
depth_distr + geom_freqpoly(mapping = aes(y = ..density.., color = cut), binwidth = 0.1)
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_path).
qplot(x = cut, y = depth, data = diamonds, geom = "boxplot")
library(plyr)

qplot(x = carat, y = depth, data = diamonds, geom = "boxplot", group = round_any(x = carat, accuracy = 0.1, f = floor)) + xlim(c(0, 3))
## Warning: position_dodge requires constant width: output may be incorrect
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 2 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values (geom_segment).
data("mpg")

qplot(x = class, y = cty, data = mpg, geom = "jitter")
qplot(x = class, y = drv, data = mpg, geom = "jitter")
qplot(x = depth, data = diamonds, geom = "density", xlim = c(55, 70))
## Warning: Removed 45 rows containing non-finite values (stat_density).
qplot(x = depth, data = diamonds, geom = "density", xlim = c(55, 70), fill = cut, alpha = I(0.2))
## Warning: Removed 43 rows containing non-finite values (stat_density).
## Warning: Removed 1 rows containing non-finite values (stat_density).
## Warning: Removed 1 rows containing non-finite values (stat_density).

3 Dealing with Overplotting

df <- data.frame(x = rnorm(2000), y = rnorm(2000))

g <- ggplot(data = df, mapping = aes(x = x, y = y))

g + geom_point()
g + geom_point(shape = 1)
g + geom_point(shape = ".")
g + geom_point(color = "steelblue", alpha = I(1/5))
g + geom_point(color = "steelblue", alpha = I(1/10))
g <- ggplot(data = diamonds, mapping = aes(x = table, y = depth))

g + geom_point()
g + geom_jitter()
jit <- position_jitter(width = 0.5)

g + geom_jitter(position = jit)
g + geom_jitter(position = jit, alpha = I(1/10))
g <- ggplot(data = diamonds, mapping = aes(x = carat, y = price))

g + stat_bin2d()
g + stat_bin2d(bins = 10)
g + stat_bin2d(binwidth = c(0.02, 200))
g + stat_binhex()
g + stat_binhex(bins = 10)
g + stat_binhex(binwidth = c(0.02, 200))
g + geom_point() + geom_density2d()

4 Drawing Maps

library(maps)

data("us.cities")

big_cities <- subset(us.cities, pop > 500000)
qplot(long, lat, data = big_cities) + borders("state", size = 0.5)
tx_cities <- subset(us.cities, country.etc == "TX")
ggplot(tx_cities, aes(long, lat)) +
borders("county", "texas", colour = "grey70") + geom_point(colour = "black", alpha = I(0.5))