ggplot2
theme
elementsAfter today’s session you will be able to:
ggplot2
graphggplot2
ggplot2
Reviewtheme
parameters
theme
function
element_text
element_line
element_blank
theme
function once with as many element_...
functions as you need
# Make a simple scatterplot
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var)) +
geom_point() +
# Modify its theme to make the axis font size bigger
theme(axis.text = element_text(size = 20),
# Also remove the grid lines
panel.grid = element_blank())
theme
and element_...
functions are used togethertheme
argument names as you work more with ggplot2
panel.grid
panel.background
axis.line
ggplot2
theme(panel.grid = element_blank())
theme
parentheses!)?
panel.background = element_blank()
axis.line = element_line(color = "black")
theme
axis.title
= axis label text (given to labs
function)axis.text
= text on axis tick marks
theme(axis.text.x = element_text(...))
theme
function!
legend.position
legend.title
element_...
, legend.position
wants words in quotes!
"none"
, "inside"
, "left"
, etc.
legend.position = "inside"
, you need to tell it where inside
legend.position.inside = c(<x position>, <y position>)
# Example scatterplot
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var)) +
geom_point() +
# With legend in the middle of the space
theme(legend.position = "inside",
legend.position.inside = c(0.5, 0.5))
ggplot2::facet_grid()
cowplot::plot_grid()
ggplot2
has an internal way of handling this called facets
# Example scatterplot
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var)) +
geom_point() +
# Facet into rows of some other variable
facet_grid(. ~ facet_variable)
ggplot(data = penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)) +
geom_point() +
facet_grid(. ~ island) +
labs(x = "Body Mass (g)", y = "Flipper Length (mm)") +
theme(legend.position = "inside",
legend.position.inside = c(0.87, 0.85),
legend.title = element_blank(),
panel.background = element_blank())
theme
tweaks you want to make!
cowplot::plot_grid
lets you put multiple different graphs together
plot_grid
, make a multi-panel graph with these two graphs