Trulli
from today.com

Overview

In this practical you’ll practice plotting data with the amazing ggplot2 package. By the end of this practical you will know how to:

  1. Build a plot step-by-step.
  2. Use multiple geoms.
  3. Work with facets.
  4. Adjust colors and add labels.
  5. Create image files.

Tasks

A - Setup

  1. Open your dataanalytics R project. It should already have the folders 1_Data and 2_Code. Make sure that the data files listed in the Datasets section above are in your 1_Data folder.

  2. Open a new R script. At the top of the script, using comments, write your name and the date and “Plotting Practical”.

## NAME
## DATE
## Plotting Practical
  1. Save the file under the name plotting_practical.R in the 2_Code folder.

  2. Using library() load the tidyverse and ggthemes packages for this practical listed in the Functions section above. If you don’t have them installed, you’ll need to install them, see the Functions tab above for installation instructions.

# Load packages
library(tidyverse)     
library(ggthemes)
  1. For this practical, we’ll use the mcdonalds.csv data set, which contains nutrition information about items from McDonalds. Using read_csv(), load the data into R and store it as a new object called mcdonalds.
# Load mcdonalds.csv as a new object called mcdonalds
XX <- read_csv("XX/XX")
  1. Using print(), summary(), head(), and View(), explore the data to make sure it was loaded correctly.

B - Building a plot step-by-step

In this section, you’ll build the following plot step by step.

  1. Using ggplot(), create the following blank plot using the data and mapping arguments (but no geom). Use Calories for the x aesthetic and SaturatedFat for the y aesthetic
ggplot(data = mcdonalds, 
       mapping = aes(x = XX, y = XX))
  1. Using geom_point(), add points to the plot
ggplot(data = mcdonalds, 
       mapping = aes(x = XX, y = XX)) +
  geom_point()
  1. Using the color aesthetic mapping, color the points by their Category.
ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
  geom_point() 
  1. Add a smoothed average line using geom_smooth().
ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
  geom_point() +
  geom_smooth() 
  1. Oops! Did you get several smoothed lines instead of just one? Fix this by specifying that the line should have one color: "black". When you do, you should then only see one line.
ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
  geom_point() +
  geom_smooth(col = "XX") 
  1. Add appropriate labels using the labs() function.
ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
  geom_point() +
  geom_smooth(col = "XX") +
  labs(title = "XX",
       subtitle = "XX",
       caption = "XX")
  1. Finally, set the plotting theme to theme_minimal(). You should now have the final plot!
ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
  geom_point() +
  geom_smooth(col = "XX") +
  labs(title = "XX",
       subtitle = "XX",
       caption = "XX")+
  xlim(XX, XX) +
  theme_minimal()

C - Adding multiple geoms

  1. Create the following plot showing the relationship between menu category and calories
ggplot(data = mcdonalds, aes(x = XX, y = XX, fill = XX)) +
  geom_violin() +
  guides(fill = FALSE) +
  labs(title = "XX",
       subtitle = "XX")
  1. Now add + geom_jitter(width = .1, alpha = .5) to your plot, what do you see?

  2. Play around with your plotting arguments to see how the results change! Each time you make a change, run the plot again to see your new output!

  • Change the width argument in geom_jitter() to width = 0.
  • Instead of using geom_violin(), try geom_boxplot().
  • Remove the fill = Category aesthetic entirely.

D - Scaling

  1. Create the above scatterplot showing the relationship between Cholesterol and Protein starting with the template below.
ggplot(mcdonalds, aes(x = XX, 
                      y = XX)) +
  geom_point() +
  theme_minimal() +
  labs(title = "XX", 
       subtitle = "XX")
  1. Color the points according to their Calories by specifying the col aesthetic.

  2. Change the colors by including the additional module + scale_colour_gradient(low = "blue", high = "red").

  3. Customize! Look at all of the named colors in R by running colors(). Then, use two new colors in your plot.

  4. To plot Cholestoral on a range from to 0 to 1000 rather than the automatically chosen range add + scale_x_continuous(limits = c(XX, XX)) or simply + xlim(XX, XX)

  5. Finally, to reverse the order of Cholestoral, that for it to go from large to small values (Caution: the default should be to plot axes in ascending order), reverse the values in xlim(XX, XX).

E - Adding labels

Now, let’s create the following plot with additional point labels using geom_text():

  1. Start with the following template:
ggplot(mcdonalds, aes(x = XX, 
                      y = XX, 
                      col = XX)) +
  geom_point() +
  theme_minimal() +
  labs(title = "XX")
  1. Try adding labels to the plot indicating which item each point represents by adding + geom_text().

  2. Where are the labels? Ah, we didn’t tell ggplot which column in the data represents the item descriptions. Fix this by specifying the label aesthetic in your first call to the aes() function. That is, include label = Item underneath the line col = XX. Now you should see lots of labels!

  3. Using the data argument in geom_text(), specify that the labels should only apply to items over 1100 calories (hint: geom_text(data = mcdonalds %>% filter(XX > XX)))

F - Create facets

  1. Now with the previous scatter plot of Sugars and Calories introduce facets according to Category by adding + facet_wrap(~ XX).

  2. With the same plot, instead of + facet_wrap(~ XX) try + facet_grid(XX ~ XX) to facet according to two variables in a cross-tabular fashion. As facet variables use two logical statements, namely wheher TotalFat is larger than 20 and whether Cholesterol is larger than 50. (Hint: + facet_grid(XX > XX ~ XX > XX).

  3. Finally, if for you the labels didn’t fid the facet panels, correct this by setting the size argument inside geom_text() to a small value (e.g., 2).

G - Customize plots using theme() facets

  1. Now a few aesthetic aspecs in the previous plot using the theme(). We still want to use theme_minimal(), but make a few adjustments. Before we get to that, however, first store your plot in an object called mcdonalds_gg using the template below.
mcdonalds_gg <- ggplot(...) + ... # Replace by your plotting code
  1. Ok, let’s start by making the axis titles more legible using the axis.title argument and the element_text() helper function. See template.
mcdonalds_gg + theme(XX = element_text(size = XX))
  1. Now in addition give the facet headers a grey (e.g., grey75) backround and its border to 'white' using the strip.background argument and element_rect() and change the color of the header text to white (using strip.text).
mcdonalds_gg + theme(XX = element_text(XX = XX),
                     XX = element_rect(XX = XX, XX = XX))

G - Saving plots

  1. It’s time to save your favorite plot to an image file! Pick your favorite plot you’ve created so far. Then, assign the plot to a new object called mcdonalds_favorite.

  2. Evaluate your mcdonalds_favorite object to see that it does indeed contain your plot.

  3. Save your plot to a .pdf-file called mcdonalds.pdf using ggsave(). When you finish, find your plot in 3_Figures and open it to see how it looks!

# Save mcdonalds_gg to a pdf file
ggsave(filename = "mcdonalds.pdf",
       path = '3_Figures',
       device = "pdf", 
       plot = mcdonalds_gg,
       width = 4, 
       height = 4, 
       units = "in")
  1. Play around with the width and height arguments to change the dimensions of the plot.

  2. Customize your code to create a jpeg image called mcdonalds.jpeg

Datasets

File Rows Columns
mcdonalds.csv 260 24

First 5 rows and columns of mcdonalds.csv

Category Item ServingSize Calories CaloriesfromFat
Breakfast Egg McMuffin 4.8 oz (136 g) 300 120
Breakfast Egg White Delight 4.8 oz (135 g) 250 70
Breakfast Sausage McMuffin 3.9 oz (111 g) 370 200
Breakfast Sausage McMuffin with Egg 5.7 oz (161 g) 450 250
Breakfast Sausage McMuffin with Egg Whites 5.7 oz (161 g) 400 210

Functions

Packages

Package Installation
tidyverse install.packages("tidyverse")
ggthemes install.packages("ggthemes")

Resources

Documentation

Cheatsheets


from R Studio