from today.com
In this practical you’ll practice plotting data with the amazing ggplot2 package. By the end of this practical you will know how to:
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.
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
Save the file under the name plotting_practical.R in the 2_Code folder.
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)
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")
print(), summary(), head(), and View(), explore the data to make sure it was loaded correctly.In this section, you’ll build the following plot step by step.

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 aestheticggplot(data = mcdonalds,
mapping = aes(x = XX, y = XX))
geom_point(), add points to the plotggplot(data = mcdonalds,
mapping = aes(x = XX, y = XX)) +
geom_point()
color aesthetic mapping, color the points by their Category.ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
geom_point()
geom_smooth().ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
geom_point() +
geom_smooth()
"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")
labs() function.ggplot(mcdonalds, aes(x = XX, y = XX, col = XX)) +
geom_point() +
geom_smooth(col = "XX") +
labs(title = "XX",
subtitle = "XX",
caption = "XX")
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()
ggplot(data = mcdonalds, aes(x = XX, y = XX, fill = XX)) +
geom_violin() +
guides(fill = FALSE) +
labs(title = "XX",
subtitle = "XX")
Now add + geom_jitter(width = .1, alpha = .5) to your plot, what do you see?
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!
width argument in geom_jitter() to width = 0.geom_violin(), try geom_boxplot().fill = Category aesthetic entirely.
Cholesterol and Protein starting with the template below.ggplot(mcdonalds, aes(x = XX,
y = XX)) +
geom_point() +
theme_minimal() +
labs(title = "XX",
subtitle = "XX")
Color the points according to their Calories by specifying the col aesthetic.
Change the colors by including the additional module + scale_colour_gradient(low = "blue", high = "red").
Customize! Look at all of the named colors in R by running colors(). Then, use two new colors in your plot.
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)
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).
Now, let’s create the following plot with additional point labels using geom_text():

ggplot(mcdonalds, aes(x = XX,
y = XX,
col = XX)) +
geom_point() +
theme_minimal() +
labs(title = "XX")
Try adding labels to the plot indicating which item each point represents by adding + geom_text().
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!
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)))
Now with the previous scatter plot of Sugars and Calories introduce facets according to Category by adding + facet_wrap(~ XX).
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).
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).
theme() facetstheme(). 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
axis.title argument and the element_text() helper function. See template.mcdonalds_gg + theme(XX = element_text(size = XX))
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))
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.
Evaluate your mcdonalds_favorite object to see that it does indeed contain your plot.
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")
Play around with the width and height arguments to change the dimensions of the plot.
Customize your code to create a jpeg image called mcdonalds.jpeg
| 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 |
| Package | Installation |
|---|---|
tidyverse |
install.packages("tidyverse") |
ggthemes |
install.packages("ggthemes") |
The main ggplot2 webpage at http://ggplot2.tidyverse.org/ has great tutorials and examples.
Check out Selva Prabhakaran’s website for a nice gallery of ggplot2 graphics http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
ggplot2 is also great for making maps. For examples, check out Eric Anderson’s page at http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
from R Studio