Pets, again, because we are going to start with the example graph from last time.
pets <- read_csv("https://psyteachr.github.io/reprores-v3/data/pets.csv")
glimpse(pets)
## Rows: 800
## Columns: 6
## $ id <chr> "S001", "S002", "S003", "S004", "S005", "S006", "S007", "S008"…
## $ pet <chr> "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog", "dog",…
## $ country <chr> "UK", "UK", "UK", "UK", "UK", "UK", "UK", "UK", "UK", "UK", "U…
## $ score <dbl> 90, 107, 94, 120, 111, 110, 100, 107, 106, 109, 85, 110, 102, …
## $ age <dbl> 6, 8, 2, 10, 4, 8, 9, 8, 6, 11, 5, 9, 1, 10, 7, 8, 1, 8, 5, 13…
## $ weight <dbl> 19.78932, 20.01422, 19.14863, 19.56953, 21.39259, 21.31880, 19…
Below is the code from where we left off last time (from my complete example of the exercise). Let’s make a few more changes to this graph:
geom_smooth to extend the line the
entire length of the x-axis.ggtext package to add color-coding to the title
of the graph, effectively moving the legend into the title.Documentation for ggtext is here: https://wilkelab.org/ggtext/. (You can hold
Ctrl and click on links in RStudio to open them in
browser.)
HINT: You will have to use the
element_markdown argument for the plot title, instead of
the element_text argument.
HINT 2: This code will be useful for editing the title’s color: TEXT HERE
final_plot <- ggplot(pets, aes(x = age, y = weight, color = pet)) +
geom_jitter(alpha = 0.6, shape = 16) +
geom_smooth(method = "lm", fullrange = TRUE) +
theme_minimal() +
theme(
plot.title = ggtext::element_markdown(hjust = 0.5, size = 16),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(face = "bold"),
axis.title = element_text(size = 14.5),
legend.position = "none"
) +
labs(
x = "Age",
y = "Weight",
title = "Age–Weight Correlation (<span style='color:chartreuse4;'>Cat</span>, <span style='color:orchid;'>Dog</span>, <span style='color:darkblue;'>Other</span>)"
) +
scale_color_manual(values = c("chartreuse4", "orchid", "darkblue"))
final_plot
Check the documentation for corrplot and generate a
correlation plot with (a) only the lower left part of
the correlation table displayed and (b) shaded cells, rather than
circles, to indicate the strength and direction of correlation.
corr_matrix <- cor(pets[4:6]) # only columns 4-6 are numeric; you will need this correlation matrix as the first argument in corrplot
corrplot::corrplot(
corr_matrix,
method = "color",
type = "lower"
)
The chunk below fits a statistical model in which country, pet, and their interaction predict score.
model <- lm(score ~ country * pet, data = pets)
data_to_graph <- as.data.frame(emmeans::emmeans(model, ~ country * pet))
ggplot(data_to_graph, aes(x = pet, y = emmean, color = country, group = country)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(
aes(ymin = emmean - SE, ymax = emmean + SE),
width = 0.1
) +
theme_minimal() +
labs(
x = "Pet",
y = "Estimated Mean Score",
color = "Country"
)
Here, use an argument from the performance package to
check the assumptions of the model fitted above. HINT: You can type
performance:: inside a code chunk and a full list of the
functions in the package will appear.
Make edits to the code chunk settings (e.g., adjusting the figure
width and/or figure height) to make the graph produced by
performance readable.
performance::check_model(model)
The ggemmeans function was used above to create a new
object in the environment called data_to_graph. View
this dataframe by running view(data_to_graph) or just
clicking on it in the environment.
Below, see some starter code for a point and 95% CI graph. You’ll see that there are some issues here. Try to:
ggplot(
data = data_to_graph,
aes(x = pet, y = emmean, color = country)
) +
geom_point(
position = position_dodge(width = 0.3),
size = 3
) +
geom_errorbar(
aes(ymin = lower.CL, ymax = upper.CL),
position = position_dodge(width = 0.3),
width = 0,
linewidth = 1
) +
theme_minimal()
A few more touches (we’re not going to give this graph the full treatment):
coord_flip function to the ggplot object. What
do you think of this alternate option? If it’s not great in this case,
could it be more useful in other instance?ggplot(
data = data_to_graph,
aes(x = pet, y = emmean, color = country)
) +
geom_point(
position = position_dodge(width = 0.3),
size = 3
) +
geom_errorbar(
aes(ymin = lower.CL, ymax = upper.CL),
position = position_dodge(width = 0.3),
width = 0,
linewidth = 1
) +
theme_minimal() +
labs(
x = "Pet",
y = "Estimated Mean Score",
color = "Country"
) +
coord_flip()
The following (relatively nonsensical) graph shows the relationship between weight and score, with age (including only pets whose age ranges from 3-9) differentiated by colour. Browse through the paleteer help page (https://r-graph-gallery.com/color-palette-finder.html) to find and apply a different colour palette to the printed graph (please edit the chunk below).
You shouldn’t have to download any additional packages besides the paletteer package.
ggplot(
data = pets[pets$age %in% c(3:9), ],
aes(x = weight, y = score, colour = as.factor(age))
) +
geom_point() +
paletteer::scale_colour_paletteer_d("ggthemes::Tableau_10") +
theme_minimal()