NOTE: You might look at the cheatsheet for ggplot2 under the module “Cheatsheets”.

Load Data

Pets, again! It’s a good toy dataset for plotting.

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…

Step-By-Step Graph Building

Plot points in a scatterplot for age (x-axis) and weight (y-axis). Map the color aesthetic onto pet.

ggplot(pets, aes(weight, score, color = pet)) +
  geom_point()

Now, add an additional geom function to graph the linear relationship between age and weight, again mapping the color aesthetic onto pet.

Note: You can save a ggplot as an object and then add additional elements to it, but for this exercise, I want you to copy and paste to start each new code chunk, then make edits. In practice, you would only have a single copy of the code for the entire plot.

ggplot(pets, aes(weight, score, color = pet)) +
  geom_point() +
  geom_smooth(aes(fill = pet), formula = y ~ x, metho= "lm)")+
  geom_hline(yintercept=100, size = 1, colour = "gray32")

You should see a small positive relation between age and weight. But, what if we wanted to visually inspect the difference in these relations between countries (Netherlands and United Kingdom)? Use facet to split the graph into two.

Also, while you’re at it, apply theme_minimal() to the entire graph.

ggplot(pets, aes(weight, score, color = pet)) +
  geom_point() +
  geom_smooth(aes(fill = pet), formula = y ~ x, metho= "lm)")+
  geom_hline(yintercept=100, size = 1, colour = "gray32")+ 
  facet_wrap(~ country) +
  theme_minimal()

On visual inspection, does there appear to be any difference for any of the pet types between the Netherlands and the United Kingdom? Maybe, but let’s decide to not keep the facet active–let’s just save it for later by typing a hash in front of the facet line of code to “deactivate” it.

Next, provide descriptive labels for the x-axis, y-axis, title, and legend.

ggplot(pets, aes(x = age, y = weight, color = pet)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  theme_minimal() +
  labs(
    x = "Age",
    y = "Weight",
    color = "Pet Type",
    title = "Relationship Between Age and Weight by Pet Type"
  )

This graph probably looks better, but there is some tweaking to do. Use theme() arguments to do the following

  1. Center the graph title and increase its size to 16.
  2. Remove the minor gridlines only (hint: for removing most things, element_blank() is your friend)
  3. Move the legend position to the coordinates c(.1, .1) then make adjustments to get the legend into the top-left area of the graph (the slightly empty area).
  4. Bold the numbers on both axes.
  5. Increase the size of the axis titles to 14.5.

NOTE: Remember that theme adjustments are not individual functions that you add to your ggplot object. Instead, theme() is a single function with lots of arguments in side it. Arguments are separated by commas, not + signs.

ggplot(pets, aes(x = age, y = weight, color = pet)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    x = "Age",
    y = "Weight",
    color = "Pet Type",
    title = "Relationship Between Age and Weight by Pet Type"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16),
    panel.grid.minor = element_blank(),
    legend.position = c(0.1, 1),
    legend.justification = c(0, 1),
    axis.text = element_text(face = "bold"),
    axis.title = element_text(size = 14.5)
  )

Okay, these are generally good changes. But, maybe the legend is a little too crammed into that corner; let’s give up and restore the default by removing the legend.position argument.

Now, a few final tweaks:

  1. There is some overlap between the points. Use both of the approaches we learned to make the overlapping points more visible.
  2. Then, change the shape of the points to #16 instead of the default (which is #19).
  3. Add an argument at the end of your code to define new colors, instead of the default red/blue/green.
  4. (Optional) Add a rug plot in the margins.
ggplot(pets, aes(x = age, y = weight, color = pet)) +
  geom_point(
    shape = 16,
    alpha = 0.6,
    position = position_jitter(width = 0.2, height = 0)
  ) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_rug(alpha = 0.2) +
  labs(
    x = "Age",
    y = "Weight",
    color = "Pet Type",
    title = "Relationship Between Age and Weight by Pet Type"
  ) +
  scale_color_manual(
    values = c(
      cat = "#E76F51",
      dog = "#2A9D8F",
      ferret = "#264653"
    )
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16),
    panel.grid.minor = element_blank(),
    axis.text = element_text(face = "bold"),
    axis.title = element_text(size = 14.5)
  )

Saving the Figure

You’re done! Save your finished plot into the “figures” folder twice: once as a .jpg with 600 DPI and once as a .svg file. Adjust the height and width until you are satisfied with the proportions. Note that, for publishing purposes, SVG files will come out more nicely, as they are vector-style graphics rather than raster-style graphics.

ggsave(
  filename = "figures/age_weight_pet_plot.jpg",
  dpi = 600,
  width = 8,
  height = 6
)

ggsave(
  filename = "figures/age_weight_pet_plot.svg",
  width = 8,
  height = 6
)