Making you data come to life!
ggplot2 and it’s grammar of graphics to create amazing figuresggplot2, a tidyverse package
ggrepel (for text labels) and ggpubr (for publication-ready plots) or patchwork (for combining multiple plots)ggplot2 is a lot more useful and user friendly than base R, making plots look a lot nicer and with more options for building and displaying graphicsmtcars dataset!ggplot() is the main function, and this creates the initial ggplot object where we then add multiple layersgeom_point(), which adds a scatter plotaes(). This is how variables (columns) in the input data are mapped to visual, or “aesthetic”, properties.ggplot() function, or local aesthetics in individual layers (such as in geom_point()).theme_set() functioncolour aesthetic (or color if you want to spell it wrong…)size or shape aestheticcyl == 4 being TRUE or FALSE)mpg data:
displ) and highway mileage (hwy). What relationship do you see?class). Does adding color help you notice anything?facet_grid()geom_point(), but there are naturally many more geoms we can usegeom_smooth() draws a smoothed line based on the trend of the provided datastat argument of different plot types (geom functions) specifies the statistical transformationgeom_bar() uses stat = "count" as it’s default to create counts of the mapped variable (as in what a bar chart does):position argument can control how geoms occupy spacelabs()mpg), build on your earlier scatterplot of displ vs hwy by adding a trend line.drv).position to fill or dodge. What changes?geom_text() and geom_label()ggrepl package can help make more legible labelsggplot(data = mtcars, mapping = aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
ggrepel::geom_text_repel(
aes(
label = rownames(mtcars)
),
max.overlaps = 100
)
# Label only a subset
ggplot(data = mtcars, mapping = aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
ggrepel::geom_text_repel(
aes(
label = rownames(mtcars[1:3, ])
),
data = mtcars[1:3, ]
)

coord_cartesian() (do this!!)
This is the RIGHT WAY, using coord_cartesian()

This is also not ideal, as it removes data outside the limits!
ggplot2 automatically adds default scales behind the scenesx_, y_, colour_, etc) and the name of the scale (continuous, discrete)# Adding text to labels
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
scale_y_continuous(
breaks = seq(0, 350, by = 50),
labels = paste0(
"HP ",
seq(0, 350, by = 50)
)
)
# No labels
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
scale_y_continuous(
breaks = seq(0, 350, by = 50),
labels = NULL
)

guides() function to control the legend displayggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
scale_colour_manual(values = c("black", "pink", "turquoise"))
# Explicitly setting the values to colours
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl))) +
scale_colour_manual(
values = c(
`4` = "black",
`6` = "pink",
`8` = "red"
)
)

ggsave()ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl)))
# Save the last printed plot
ggsave(filename = "my_plot_1.pdf")
# Save the plot to a variable first
plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl)))
ggsave(filename = "my_plot_1.png", plot)mpg dataset, label only cars with hwy > 35 & displ < 2, using geom_text_repel() so labels don’t overlap.mpg, set x breaks at integers and y limits to 10–50.
geom_jitter is like geom_point, but adds noise so point aren’t on top of each other, handy for mapping the raw data onto other geoms!plotly can be used to make 3D plots, but this is very situational and should probably only be done in cases where the plot is intended to be interactiveggstatsplotDon’t try to memorise everything, look stuff up!
ggplot2 cheat sheet: http://rstudio.com/resources/cheatsheets
A book on ggplot2 by the authors: https://ggplot2-book.org
Another nice book: https://rkabacoff.github.io/datavis/index.html

MET581 - Data Visualisation in R - Slides and code avilable here