Directions

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Moodle. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

Questions

Vis 1 This graphic is a traditional stacked bar chart. This graphic works on the mpg dataset, which is built into the ggplot2 library. This means that you can access it simply by ggplot(mpg, ….). There is one modification above default in this graphic, I renamed the legend for more clarity.

# place the code to import graphics here
library(ggplot2)
p1 = ggplot(mpg, aes(x = class, fill = trans)) + 
     geom_bar() + 
     scale_fill_discrete(name = "Transmission")
p1

Vis 2 This boxplot is also built using the mpg dataset. Notice the changes in axis labels, and an altered theme_XXXX

# place the code to import graphics here
p2 = ggplot(mpg, aes(x = manufacturer, y = hwy)) + 
     geom_boxplot() + 
     coord_flip() + 
     xlab("Vehicle Manufacturer") + 
     ylab("Highway Fuel Efficiency (miles/gallon)") + 
     theme_classic()
p2

Vis 3 This graphic is built with another dataset diamonds a dataset also built into the ggplot2 package. For this one I used an additional package called library(ggthemes) check it out to reproduce this view.

# place the code to import graphics here
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.6.1
p3 = ggplot(diamonds, aes(x = price, fill = cut, color = cut)) + 
     geom_density(alpha = 0.3, size = 0.8) + 
     ggtitle("Diamond Price Density") +
     xlab("Diamond Price (USD)") + 
     ylab("Density") + 
     theme_economist()
p3

Vis 4 For this plot we are changing vis idioms to a scatter plot framework. Additionally, I am using ggplot2 package to fit a linear model to the data all within the plot framework. Three are edited labels and theme modifications as well.

# place the code to import graphics here
p4 = ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + 
     geom_point() + 
     geom_smooth(method = 'lm') + 
     ggtitle("Relationship between Petal and Sepal Length") + 
     xlab("iris Sepal Length") + 
     ylab("iris Petal Length")
p4

Vis 5 Finally, in this vis I extend on the last example, by plotting the same data but using an additional channel to communicate species level differences. Again I fit a linear model to the data but this time one for each species, and add additional theme and labeling modicitations.

# place the code to import graphics here
p5 = ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + 
     geom_point() + 
     geom_smooth(method = 'lm', se = FALSE) + 
     ggtitle("Relationship between Petal and Sepal Length", subtitle = "Species level comparison") +      xlab("iris Sepal Length") + 
     ylab("iris Petal Length") +
     theme_tufte() +
     guides(color=FALSE)
p5