Angela Zoss
February 9, 2017
an R package designed to create plots based on a theory of the grammar of graphics.
Note: some geoms also include
data summary functions.
e.g., the “bar” geom will count
data points in each category.
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point()
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class))
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
labs(x = "Engine Displacement, in Liters", y="Highway Miles per Gallon")
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
labs(x = "Engine Displacement, in Liters", y="Highway Miles per Gallon") +
theme_bw()
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class), size=7) +
labs(x = "Engine Displacement, in Liters", y="Highway Miles per Gallon") +
theme_bw()
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class), size=7, alpha=0.5) +
labs(x = "Engine Displacement, in Liters", y="Highway Miles per Gallon") +
theme_bw()
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class), size=7, alpha=0.5) +
labs(x = "Engine Displacement, in Liters", y="Highway Miles per Gallon") +
scale_color_brewer(palette="Dark2", name="") +
theme_bw()
# geom_bin2d will aggregate points for you
# using scale_?_log10 will change the axis spacing
# but leave labels comprehensible
ggplot(diamonds, aes(carat, price)) +
geom_bin2d() +
scale_x_log10() +
scale_y_log10()
Order by semantics
data$answer <-
factor(data$answer,
levels=c("None","A little", "Some", "A lot"),
ordered = TRUE)
Order by value
data$academic_field <-
factor(data$academic_field,
levels=names(
sort(
table(
data$academic_field),decreasing=TRUE)))
coord_flip()
Oops!
data$academic_field <-
factor(data$academic_field,
levels=names(
sort(
table(data$academic_field),
decreasing=TRUE)))
data$academic_field <-
factor(data$academic_field,
levels=names(
sort(
table(data$academic_field))))
fill=highest_degree
facet_grid(.~highest_degree)
scale_x_discrete(drop=FALSE)
scale_y_continuous(limits=c(0,40),breaks=c(0,10,20,30,40),minor_breaks=NULL)
scale_fill_manual(
values=c("snow4","snow3",
"tan3","tan1",
"turquoise2","turquoise4"))
scale_fill_manual(
values=c("#fee391","#fe9929", "#cc4c02"))
# Also see package RColorBrewer
scale_fill_brewer(palette="BrBG")