title: “Introduction to GGplot2 & Friends”
output: pdf_document

Meseret

General instructions for labs:

As a starter, please remember to load the ggplot2 library (or to install it if you haven’t already).

Examples with the “midwest” data

ggplot makes it easy to edit labels and make a title.

For example, let’s look at the midwest data set, which contains demographic information of midwest counties.

library(dplyr); library(ggplot2)
head(midwest)
dim(midwest)

We should be comfortable making a scatter plot. Here’s one of percbelowpoverty by percollege.

ggplot(midwest, aes(x = percollege, y = percbelowpoverty)) + 
  geom_point()

Basic thematic tweaks

Unfortunately, while it is clear what they are representing, the variable labels are not pleasing to read on a plot. We can change the labels using the xlab() and ylab() commands, and can add a title using ggtitle().

ggplot(midwest, aes(x = percollege, y = percbelowpoverty)) + 
  geom_point() + 
  xlab("Percent college educated") + 
  ylab("Percent below the poverty line") + 
  ggtitle("Education % versus poverty % among 437 midwest counties")

Exercise 1:

Describe what the following graph is showing, and add appropriate labels and a title

ggplot(midwest, aes(x = percollege, colour = state)) + 
  geom_density() + 
  xlab("") + 
  ylab("Percent") + 
  ggtitle("")

ggplot(midwest, aes(x = percollege, colour = state)) + geom_density() + xlab(“Percent College Educated”) + ylab(“Density”) + ggtitle(“Distribution of College Education by State”)

The graph shows the distribution of the percentage of college-educated adults across the Midwestern states. Each colored curve represents a different state. The graph allows us to compare the distribution of college education levels among the states. ## Exercise 2: Practice with the Fuel Economy Data:

Refer to the power point presentation examples on the mpg data set. Reproduce the five charts shown in slides 11 TO 25. Typeset the code you see in the slide and generate the plot. Add a main title and customize the labels on the axes to improve each plot. Finally, write a short interpretation of each plot, and type one weakness(apart from the labels and theme) that hinder how one may interpret the visualization if applicable. ggplot(mpg, aes(x = manufacturer)) + geom_bar() + labs( title = “Number of Cars by Manufacturer”, x = “Manufacturer”, y = “Number of Cars” ) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) The dataset does not contain an equal number of observations for every manufacturer, so the number of cars shown does not represent the actual number of vehicles ggplot(mpg, aes(x = model)) + geom_bar() + labs( title = “Number of Cars by Model”, x = “Car Model”, y = “Number of Cars” ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) There are many model categories, making the chart crowded and difficult to compare visually. ggplot(mpg, aes(x = displ)) + geom_histogram(binwidth = 0.5) + labs( title = “Distribution of Engine Displacement”, x = “Engine Displacement (Litres)”, y = “Number of Cars” ) There are fewer observations for cars with large engine displacement. ggplot(mpg, aes(x = factor(year))) + geom_bar() + labs( title = “Number of Cars by Model Year”, x = “Model Year”, y = “Number of Cars” ) Because only two model years are represented, this plot cannot be used to describe a continuous trend in car characteristics over the entire 1999–2008 period. ggplot(mpg, aes(x = factor(cyl))) + geom_bar() + labs( title = “Number of Cars by Number of Cylinders”, x = “Number of Cylinders”, y = “Number of Cars” ) The plot shows that cars with different numbers of cylinders are not equally represented. Four- and six-cylinder vehicles account for many observations, while other cylinder counts occur less frequently. install.packages(“tinytex”) tinytex::install_tinytex()