Data: Your input data (in long format)
Aesthetics: what makes your data visible, e.g., size, line color, variables to plot, fill color, line type, transparency, etc.
Geometry: determines the type of plot.
Statistics: statistical transformation of continuous data
Facets: for splitting plot into subplots.
Coordinates: Numeric systems to limit, breakdown, transform position of geometry.
Themes: Overall visual of plots and customization.
ggplot()
aes()
geom()
# Load library
library(ggplot2)
# Define data and global aesthetics
ggplot(diamonds, aes(x = carat, y = price, color = color)) +
geom_point(alpha = 0.8) +
stat_smooth(color = "black", linewidth = 0.8) +
facet_grid(cut ~ color) +
scale_y_continuous(breaks = seq(from = 0, to = 20000, by = 10000)) +
theme_bw()
Aesthetics aes()
describe how variables map to visual properties or aesthetics.
The position of data points are described by values from x
and y
shape, size, or color styles can also be specified in aes()
.
A variable or a set of value you can measure.
Continuous data values are values you can arbitrarily fine intermediates.
Age, height, BMI, date, assignment score, etc.
Sometimes, series of continuous variable can be a discrete variable.
A variable or a set of value you can count.
Grade (A B C D), Name, Type, number of person in a room, etc.
Sometimes a vector of a discrete variable can be classified into a kind of ordinal number. That may required before time-series plotting.
Frequently used geoms (Explore more plot in R Graph Gallery: https://r-graph-gallery.com)
Date scales behave like numeric scales, it’s ordinal, but is often more convenient to use the date_labels
argument with the predefined formats. More available formatting strings: https://ggplot2-book.org/scales-position.html#sec-date-labels.
Available color palettes from package colorBlindness
.
More information on R colorBlindness package: https://cran.r-project.org/web/packages/colorBlindness/vignettes/colorBlindness.html
viridis
color paletteserupt <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster() + scale_x_continuous(NULL, expand = c(0, 0)) + scale_y_continuous(NULL, expand = c(0, 0))
# Plot
erupt
erupt + scale_fill_viridis_c(option = "viridis")
erupt + scale_fill_viridis_c(option = "magma")
erupt + scale_fill_viridis_c(option = "plasma")
erupt + scale_fill_viridis_c(option = "rocket")
erupt + scale_fill_viridis_c(option = "turbo")
distiller
color paletteserupt + scale_fill_distiller(palette = "RdBu")
erupt + scale_fill_distiller(palette = "Pastel1")
erupt + scale_fill_distiller(palette = "OrRd")
The distiller scales applied brewer color palettes by by smoothly interpolating 7 colors from any palette to a continuous scale. For more brewer color palettes, see https://colorbrewer2.org.
ggsci
color paletteslibrary(ggsci)
dt_hm <- scale(as.matrix(mtcars)[1:10, ], center = TRUE, scale = TRUE)
p_hm <- as.data.frame(dt_hm) %>% rownames_to_column(var = "cars") %>%
pivot_longer(!cars) %>%
ggplot(aes(x = name, y = cars, fill = value)) +
geom_tile(color = "black") +
coord_equal() +
labs(x=NULL, y = NULL) +
theme(legend.position = "none",
axis.text.x = element_blank())
p_hm
p_hm + scale_fill_gsea()
p_hm + scale_fill_material("yellow")
p_hm + scale_fill_material("grey")
Discover more continuous ggsci color palette: https://cran.r-project.org/web/packages/ggsci/vignettes/ggsci.html
paletteer
color paletteserupt + scale_fill_paletteer_c("ggthemes::Green-Blue Diverging")
erupt + scale_fill_paletteer_c("ggthemes::Red-Blue-White Diverging")
erupt + scale_fill_paletteer_c("ggthemes::Temperature Diverging")
erupt + scale_fill_paletteer_c("grDevices::rainbow")
erupt + scale_fill_paletteer_c("grDevices::heat.colors")
erupt + scale_fill_paletteer_c("grDevices::Viridis")
More continuous paletteer color palettes can be found at: https://pmassicotte.github.io/paletteer_gallery.
RColorBrewer
palettesbars + scale_fill_brewer(palette = "BrBG")
bars + scale_fill_brewer(palette = "RdYlGn")
bars + scale_fill_brewer(palette = "Dark2")
Interactive RColorBrewer picker: https://colorbrewer2.org
ggsci
palettesggsci
offers high-quality color palettes based on color schemes used in scientific journals, data visualization libraries, and science fiction movies.
paletteer
bars + scale_fill_paletteer_d("awtools::bpalette")
bars + scale_fill_paletteer_d("basetheme::ink")
bars + scale_fill_paletteer_d("calecopal::kelp1")
bars + scale_fill_paletteer_d("fishualize::Centropyge_loricula")
Interactive discrete paletteer color palette: https://emilhvitfeldt.github.io/r-color-palettes/discrete.html
The alpha scale maps shade transparency to a numerical value.
ggplot2: Elegant Graphics for Data Analysis (3e): written by Hadley Wickham, Danielle Navarro, and Thomas Lin Pedersen (2023).
Introduction to data visualisation with ggplot2 Workshop: by QCBS R Workshop Series, 2023-04-24
Fundamentals of Data Visualization: by Claus O. Wilke, 2019
17 May 2023