Objective

The objective of this problem set is to use the ggplot2 package for data visualization.

Vis 1

This graphic is a traditional stacked bar chart on the mpg dataset, I have renamed the legend entry to reflect in the grpahic.

library(ggplot2)
ggplot(data = mpg, aes(x = class, fill = trans)) +
  geom_bar() +
  labs(fill = "Transmission")

Vis 2

This boxplot is also based on the mpg dataset. Here, the axis are labled and the theme of the graph is also modified.

ggplot(data = mpg, aes(x = manufacturer, y = hwy)) +
  geom_boxplot() + 
  coord_flip() + 
  labs(x = "Vehicle Manufacturer", y = "Highway Fuel Efficiency (miles/gallon)") +
  theme_classic()

Vis 3

This graphic is built with diamonds dataset. I have utilized library(ggthemes) package to create the below graph.

library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.4.3
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(aes(fill = cut, colour = cut), size = 0.6, alpha = 0.2) +
  ggtitle("Dimond Price Density") +
  labs(y = "Density", x = "Diamond Price (USD)") +
  theme_economist() 

Vis 4

For this plot, I have worked with the iris data set. I am using a scatter plot framework and ggplot2 package to fit a linear model to the data all within the plot framework. It also consists of modified lables and themes.

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point() +
  stat_smooth(method = "lm") +
  ggtitle("Relationship between Petal and Sepal Length") +
  labs(x = "Iris Sepal Length", y = "Iris Patal Length") +
  theme_minimal()

Vis 5

This vis extends on the last example. It plots the same data based on species level differences. I fit a linear model to the data with separating the data for for each species. Also, the theme and labels are modified.

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, colour = Species)) +
  geom_point(size = 1.5) +
  geom_smooth(method = "lm", size = 1, se = FALSE) +
  labs(title = "Relationship between Petal and Sepal Length", subtitle = "Species level comparison", x = "Iris Sepal Length", y = "Iris Patal Length") +
  theme_tufte() +
  theme(legend.position = "bottom")