# please do not alter this code chunk knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, error = TRUE) library(tidyverse) library(patchwork) library(reprores) # install the class package reprores to access built-in data # devtools::install_github("psyteachr/reprores-v2) # or download data from the website # https://psyteachr.github.io/reprores/data/data.zip
Generate the plots below showing the distribution of dog weight by breed.
# do not edit this chunk # dog weights estimated from http://petobesityprevention.org/ideal-weight-ranges/ set.seed(0) # makes sure random numbers are reproducible dogs <- tibble( breed = rep(c("beagle", "boxer", "bulldog"), each = 100), weight = c( rnorm(100, 24, 6), rnorm(100, 62.5, 12.5), rnorm(100, 45, 5) ) )
Make a violin plot with breed
on the x-axis and weight
on the y-axis. Make each breed white, 50% transparent, and outlined in a different colour, but omit the legend.
dog_violin <- ggplot(dogs, aes(breed, weight, colour = breed)) + geom_violin(fill = "white", alpha = .5, show.legend = FALSE) dog_violin # prints the plot below
Make a boxplot with breed
on the x-axis and weight
on the y-axis. Make each breed white, 50% transparent, and outlined in a different colour, but omit the legend.
dog_boxplot <- ggplot(dogs, aes(breed, weight, colour = breed)) + geom_boxplot(fill = "white", alpha = .5, show.legend = FALSE) dog_boxplot # prints the plot below
Make a density plot with weight
on the x-axis. Make each breed white, 50% transparent, and outlined in a different colour, but omit the legend.
dog_density <- ggplot(dogs, aes(weight, colour = breed)) + geom_density(fill = "white", alpha = .5, show.legend = FALSE) dog_density # prints the plot below
Use stat_summary
to create a column plot with breed
on the x-axis and weight
on the y-axis and error bars showing 1 standard error. Make each breed white, 50% transparent, and outlined in a different colour, but have the error bars be black. Omit the legend.
dog_column <- ggplot(dogs, aes(breed, weight, colour = breed)) + stat_summary(geom = "col", fill = "white", alpha = .5) + stat_summary(geom = "errorbar", width = 0, colour = "black") + theme(legend.position = "none") dog_column # prints the plot below
Create a grid of the violin plot, boxplot, and column plot above with all three plots on the same row. Make the figure 8 inches wide, 4 inches tall, and the image cover 100% of the width of the markdown output.
dog_violin + dog_boxplot + dog_column + patchwork::plot_layout(nrow = 1)
For the four plots above, change the axis labels so the x-axis reads "Breed of Dog" or "Weight of Dog" (depending on the plot type) and the y-axis reads "Weight of Dog", "Number of Dogs", or "Density of Dogs" (depending on the plot type).
Change the default colours to "orange", "dodgerblue", and "hotpink".
Add a title to each plot describing the plot type.
Save each plot as a PNG file with the names "dog_violin.png", "dog_boxplot.png", "dog_density.png", and "dog_column.png" (the names are important so they show up in the code below).
# Hint: you can add changes to an existing plot, e.g.: # dog_density + xlim(0, 100) colours <- scale_colour_manual(values = c("orange", "dodgerblue", "hotpink")) labs <- labs(x = "Breed of Dog", y = "Weight of Dog") v <- dog_violin + colours + labs + ggtitle("Violin Plot") ggsave("dog_violin.png") b <- dog_boxplot + colours + labs + ggtitle("Box Plot") ggsave("dog_boxplot.png") d <- dog_density + colours + labs(title = "Density Plot", x = "Weight of Dog", y = "Density of Dogs") ggsave("dog_density.png") c <- dog_column + colours + labs + ggtitle("Column Plot") ggsave("dog_column.png")
# do not change; displays the images saved above knitr::include_graphics("dog_violin.png") knitr::include_graphics("dog_boxplot.png") knitr::include_graphics("dog_density.png") knitr::include_graphics("dog_column.png")
Represent the relationships among disgust subscales from the dataset reprores::disgust_scores.
Graph the linear relationship between moral and pathogen disgust. Make sure the axes run from the minimum to maximum possible scores on both axes. Give the graph an appropriate title and axis labels.
data("disgust_scores", package = "reprores") disgust_lineplot <- ggplot(disgust_scores, aes(moral, pathogen)) + geom_smooth(formula = y~x, method = lm) disgust_lineplot # prints the plot below
If you add geom_point()
to the plot above, you'll see that there is too much data for this visualisation to make sense. Instead, create a 2d density plot of the relationship between moral and pathogen disgust for disgust_scores
.
disgust_density <- ggplot(disgust_scores, aes(moral, pathogen)) + geom_density2d_filled() disgust_density # prints the plot below
Create a heatmap of the relationships among all the questions in reprores::disgust_cors. The correlations have already been calculated for you.
Bonus: Figure out how to rotate the text on the x-axis so it's readable.
data("disgust_cors", package = "reprores") disgust_heatmap <- ggplot(disgust_cors, aes(V1, V2, fill = r)) + geom_tile() + scale_fill_viridis_c() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) disgust_heatmap # prints the plot below
Create a grid of plots for the reprores::stroop dataset using faceting.
Plot rt
for each combination of word
and ink
to make a 5x5 grid of density plots. Make each plot line the same colour as the ink. For bonus points, make the lines for plots where the ink colour matches the word colour thicker than the other lines.
data("stroop", package = "reprores") word_by_ink <- ggplot(stroop, aes(rt, colour = ink, size = ink == word)) + geom_density(show.legend = FALSE) + facet_grid(word~ink, labeller = label_both) + # sets the colour palette to custom values scale_colour_manual(values = c("blue", "brown","green", "purple", "red")) + # you may have chosen different sizes for line thickness scale_size_manual(values = c(.5, 1.5)) word_by_ink # prints the plot below
Create a 3x3 grid of linear line plots from disgust_scores
with columns representing the x-axis and rows representing the y-axis and assign it to disgust_grid
. Put a density plot of each variable along the diagonal. Make sure the graphs have appropriate titles and axis labels and that the range of the axes are the same in all graphs.
| | moral | sexual | pathogen | |--------------|---------|---------|----------| | moral | density | line | line | | sexual | line | density | line | | pathogen | line | line | density |
# Hint: set up your 9 separate plots first my_geom <- geom_smooth(formula = y~x, method = lm) # regression line plots moral_sexual <- ggplot(disgust_scores, aes(moral, sexual, color)) + ggtitle("Moral vs Sexual") + my_geom + xlim(0, 6) + ylim(0, 6) moral_pathogen <- ggplot(disgust_scores, aes(moral, pathogen)) + ggtitle("Moral vs Pathogen") + my_geom + xlim(0, 6) + ylim(0, 6) pathogen_moral <- ggplot(disgust_scores, aes(pathogen, moral)) + ggtitle("Pathogen vs Moral") + my_geom + xlim(0, 6) + ylim(0, 6) pathogen_sexual <- ggplot(disgust_scores, aes(pathogen, sexual)) + ggtitle("Pathogen vs Sexual") + my_geom + xlim(0, 6) + ylim(0, 6) sexual_moral <- ggplot(disgust_scores, aes(sexual, moral)) + ggtitle("Sexual vs Moral") + my_geom + xlim(0, 6) + ylim(0, 6) sexual_pathogen <- ggplot(disgust_scores, aes(sexual, pathogen)) + ggtitle("Sexual vs Pathogen") + my_geom + xlim(0, 6) + ylim(0, 6) # density plots moral_moral <- ggplot(disgust_scores, aes(moral)) + geom_density() + ggtitle("Moral Disgust") + xlim(0, 6) + ylim(0, 0.4) sexual_sexual <- ggplot(disgust_scores, aes(sexual)) + geom_density() + ggtitle("Sexual Disgust") + xlim(0, 6) + ylim(0, 0.4) pathogen_pathogen <- ggplot(disgust_scores, aes(pathogen)) + geom_density() + ggtitle("Pathogen Disgust") + xlim(0, 6) + ylim(0, 0.4) # add the plots together moral_moral + sexual_moral + pathogen_moral + moral_sexual + sexual_sexual + pathogen_sexual + moral_pathogen + sexual_pathogen + pathogen_pathogen + patchwork::plot_layout(ncol = 3) + patchwork::plot_annotation(tag_levels = "A")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.