Let’s start by creating a histogram for how many times Roy Kent uses his favorite word in Ted Lasso
# Creating a decent blank graph
gg_RK <-
ggplot(
data = richmondway,
mapping = aes(
x = F_count_RK
)
) +
# Having the histogram sit on the x-axis
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
) +
# Changing the labels:
labs(
x = "Number of F-Bombs",
y = "Number of Episodes",
title = "Number of Times Roy Kent uses F*^$ in an Episode of Ted Lasso"
)
# Creating the default histogram
gg_RK +
geom_histogram(bins = 10)
Not great, defaults to a grey color bar with no lines around it :(
The section below will use update_geom_defaults()
to fix
that!
geom
s that draw
rectangles# Updating the fill, color, and alpha default choices for geoms that draw rectangles
update_geom_defaults(
# Specifying a change in rectangle geometries
geom = "rect",
# Using the list() function to make changes with the new argument
new = list(
fill = "navyblue", # blue regions
color = "red", # red outline
alpha = 0.7 # very partially see-thru
)
)
Now let’s see what the histogram will look like:
gg_RK +
geom_histogram(bins = 10)
We didn’t have to specify fill
, color
, or
alpha
in geom_histogram()
since we changed the
defaults!
Other common geom
s that draw a rectangle are
geom_bar()
and geom_col()
ggplot(
data = richmondway,
mapping = aes(
x = Coaching_flag
)
) +
geom_bar() +
labs(
x = "Was Roy Kent coaching in this episode?",
y = "Episode Count"
) +
scale_y_continuous(
expand = c(0, 0, 0.05, 0)
)
Same default choices!
What if we wanted to make a density plot?
gg_RK +
geom_density()
Our updates didn’t apply to geom_density()
because it
draws a density, not a rectangle. So let’s use
update_geom_defaults()
to make a change to density!
# Updating the default fill, color, and alpha of geoms that create densities
update_geom_defaults(
geom = "density",
new = list(
color = "red",
fill = "navyblue",
alpha = 0.5
)
)
# Making the density plot
gg_RK +
geom_density()
Let’s create a boxplot:
gg_RK +
geom_boxplot() +
scale_y_continuous(
expand = c(0.05, 0, 0.05, 0),
breaks = NULL
) +
labs(y = NULL)
Defaults the line color to black and the box color to white. Let’s change them to red and navyblue, respectively:
# Updating the default color, fill, and linewidth for geom_boxplot
update_geom_defaults(
geom = GeomBoxplot, # Specify the geom almost by name
new = list(
color = "red",
fill = "navyblue",
linewidth = 1
)
)
# Same code as earlier for our box plot
gg_RK +
geom_boxplot() +
scale_y_continuous(
expand = c(0.05, 0, 0.05, 0),
breaks = NULL
) +
labs(y = NULL)
If you want to make changes to geom
s that draw lines
(geom_line()
, geom_path()
,
geom_smooth()
) we can use geom = "line"
and if
we want to make changes to points (geom_point()
and
geom_jitter()
), we can use geom = "point"
.
# Making default lines wider and dashed
update_geom_defaults(
geom = "line",
new = list(
linewidth = 1,
linetype = "dashed"
)
)
# Making default points larger and square
update_geom_defaults(
geom = "point",
new = list(
size = 3,
shape = "square"
)
)
Let’s see if there is an association between the number of Fs given and the IMDB rating
ggplot(
data = richmondway,
mapping = aes(
x = F_count_RK,
y = Imdb_rating
)
) +
# Will add a wider dashed line
geom_line() +
# Should draw larger squares instead of small points
geom_point(
alpha = 0.5
) +
# Changing the labels
labs(
x = "F-bombs used by Roy Kent",
y = "Episode IMDB Rating"
)
Doesn’t seem to be much of a pattern. The quality of the episode (judged by the IMDB rating) isn’t associated with how often Roy drops an F-bomb.
The plot below shows how often Roy Kent says F*^% compared to everyone else in the episode.