Lecture 6
Cornell University
INFO 3312/5312 - Spring 2024
February 9, 2024
Coordinate systems
Facets
Themes (a little bit)
x
and y
) to produce a 2d position on the plot:
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 (won’t be using much now that geoms can take aesthetic mappings in x and y axes)coord_fixed()
: Cartesian coordinate system with a fixed aspect ratio. (useful only in limited circumstances)coord_trans()
: Apply arbitrary transformations to x and y positions, after the data has been processed by the statcoord_polar()
: Polar coordinatescoord_map()
/ coord_quickmap()
/ coord_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 values (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values (`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 values (`stat_smooth()`).
## Warning: Removed 235 rows containing missing values (`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 values (`stat_smooth()`).
## Removed 235 rows containing missing values (`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 values (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values (`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()
ggplot(penguins, aes(x = 1, fill = species)) +
geom_bar() +
labs(title = "Stacked bar chart")
ggplot(penguins, aes(x = 1, fill = species)) +
geom_bar() +
coord_polar(theta = "y") +
labs(title = "Pie chart")
ggplot(penguins, aes(x = 1, fill = species)) +
geom_bar() +
coord_polar(theta = "x") +
labs(title = "Bullseye chart")
aside: about pie charts…
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-04
ae-04
(repo name will be suffixed with your NetID).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")