A package that is
- elegant and versatile
- grammar of graphics
- layered structure that lets you build block by block
- different from base graphics (base R)
- curve could be steep
- but looks amazing when it works!
- curve could be steep
November 30, 2022
A package that is
Understanding breakdown of your graph is important
This is core of ggplot2!
Will make plotting of perfect graph easier
Process of adding layers
Options:
library(ggplot2) library(tidyverse)
ggplot(diamonds) + geom_point(aes(x = carat, y = price, colour = cut)) + geom_smooth(aes(x = carat, y = price, colour = cut))
ggplot(diamonds) + geom_point(aes(x = carat, y = price, colour = cut)) + geom_smooth(aes(x = carat, y = price, colour = cut))
ggplot(diamonds) + geom_point(aes(x = carat, y = price, colour = cut)) + geom_smooth(aes(x = carat, y = price))
ggplot(diamonds) + geom_point(aes(x = carat, y= price, colour = cut)) + geom_smooth(aes(x = carat, y = price)) + ylim(0, 3000)
## Warning: Removed 23604 rows containing non-finite values (stat_smooth).
## Warning: Removed 23604 rows containing missing values (geom_point).
ggplot(diamonds) + geom_point(aes(x = carat, y= price, colour = cut)) + geom_smooth(aes(x = carat, y = price)) + ylim(0, 3000) + xlim(0, 1)
## Warning: Removed 23716 rows containing non-finite values (stat_smooth).
## Warning: Removed 23716 rows containing missing values (geom_point).
ggplot(diamonds) + geom_point(aes(x = carat, y= price, colour = cut)) + geom_smooth(aes(x = carat, y = price)) + coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000))
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price)) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset")
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price)) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)")
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)")
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)") +
scale_colour_brewer(palette = "Spectral")
Link to colour cheatsheet
Link to colour converter and colour numbers
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)") +
scale_colour_brewer(palette = "Spectral") +
theme(legend.position = "bottom",
legend.title = element_text(colour = "Steelblue4", face = "bold", size = 15))
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)") +
scale_colour_brewer(palette = "Spectral") +
theme(legend.position = "bottom",
legend.title = element_text(colour = "Steelblue4", face = "bold", size = 15),
legend.background = element_rect(fill = "Slategray2", size = 0.5, linetype = "solid"))
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0.2, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)") +
scale_colour_brewer(palette = "Spectral") +
theme(legend.position = "bottom",
legend.title = element_text(colour = "Steelblue4", face = "bold", size = 15),
legend.background = element_rect(fill = "Slategray2", size = 0.5, linetype = "solid")) +
scale_x_continuous(breaks = seq(0.2, 1, 0.05))
ggplot(diamonds) +
geom_point(aes(x = carat, y= price, colour = cut)) +
geom_smooth(aes(x = carat, y = price), colour = "firebrick", size = 2) +
coord_cartesian(xlim = c(0.2, 1), ylim = c(0, 3000)) +
labs(title = "Carat vs Price",
subtitle = "Diamond Dataset",
x = "Carat Diamond",
y = "Price ($)") +
scale_colour_brewer(palette = "Spectral") +
theme(legend.position = "bottom",
legend.title = element_text(colour = "Steelblue4", face = "bold", size = 15),
legend.background = element_rect(fill = "Slategray2", size = 0.5, linetype = "solid")) +
scale_x_continuous(breaks = seq(0.2, 1, 0.05)) +
scale_y_continuous(breaks = seq(0, 3000, 1000),
labels =c("free", "expensive", "more expensive", "really expensive"))