Lecture 6
Cornell University
INFO 3312/5312 - Spring 2025
February 6, 2025
Source: Andrew Van Dam
x
and y
) to produce a two-dimension position on the plot
Preserve the shape of geoms
coord_cartesian()
: the default Cartesian coordinate system, where the 2D position of an element is given by the combination of the x
and y
positions.coord_flip()
: Cartesian coordinate system with x
and y
axes flipped.coord_fixed()
: Cartesian coordinate system with a fixed aspect ratio.Can change the shapes – a straight line may no longer be straight. The closest distance between two points may no longer be a straight line.
coord_trans()
: Apply arbitrary transformations to x and y positions, after the data has been processed by the statcoord_polar()
/ coord_radial()
: Polar coordinatescoord_sf()
: Map projectionsIdentify the differences between each plot. Focus on the range of the x
and y
axes as well as the contents of the plots.
02:00
base_plot +
labs(title = "Plot 1")
base_plot +
scale_x_continuous(limits = c(190, 220)) +
scale_y_continuous(limits = c(4000, 5000)) +
labs(title = "Plot 2")
base_plot +
xlim(190, 220) +
ylim(4000, 5000) +
labs(title = "Plot 3")
base_plot +
coord_cartesian(xlim = c(190, 220),
ylim = c(4000, 5000)) +
labs(title = "Plot 4")
base_plot +
labs(title = "Plot 1")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
base_plot +
scale_x_continuous(limits = c(190, 220)) +
scale_y_continuous(limits = c(4000, 5000)) +
labs(title = "Plot 2")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 235 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 235 rows containing missing values or values outside the scale range
## (`geom_point()`).
base_plot +
xlim(190, 220) +
ylim(4000, 5000) +
labs(title = "Plot 3")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 235 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 235 rows containing missing values or values outside the scale range
## (`geom_point()`).
base_plot +
coord_cartesian(xlim = c(190, 220),
ylim = c(4000, 5000)) +
labs(title = "Plot 4")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
scale_*_continuous(limits = ...)
xlim()
and ylim()
coord_cartesian(xlim = ..., ylim = ...)
coord_fixed()
Useful when having an aspect ratio of 1 makes sense, e.g. scores on two tests (reading and writing) on the same scale (0 to 100 points)
coord_polar()
/coord_radial()
More examples: R Graph Gallery
Image credit: YouGov
What do you know about pie charts and data visualization best practices? Love ’em or lose ’em?
For categorical variables with few levels, pie charts can work well
For categorical variables with many levels, pie charts are difficult to read
ae-05
Instructions
ae-05
(repo name will be suffixed with your GitHub name).renv::restore()
to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.12:00
facet_*()
facet_wrap()
facet_grid()
for faceting
facet_null()
: a single plot, the defaultFreeing the y scale improves the display, but it’s still not satisfying. What’s wrong with it?
ggplot(penguins, aes(y = species, x = body_mass_g, fill = species)) +
geom_boxplot(show.legend = FALSE) +
facet_grid(rows = vars(island)) +
labs(title = "Same scale and spacing")
ggplot(penguins, aes(y = species, x = body_mass_g, fill = species)) +
geom_boxplot(show.legend = FALSE) +
facet_grid(rows = vars(island), scales = "free_y") +
labs(title = "Free y scale, same spacing")