Data Visualization

Modified slides from “excersises_slides-1-49.Rmd” for Quiz 7

knitr::opts_chunk$set(echo = TRUE)

Libraries

library(pacman)
p_load(tidyverse, RColorBrewer)
theme_set(theme_minimal()) # change plot theme for all plots 

Question 1

-Create a plot with the faithful dataset

-add points with geom_point

-assign the variable eruptions to the x-axis

-assign the variable waiting to the y-axis

-color the points according to whether waiting is smaller or greater than 64

ggplot(faithful) + 
  geom_point(aes(x = eruptions, y = waiting, 
                 colour = waiting > 64))

Question 2

-Create a plot with the faithful dataset -add points with geom_point

-assign the variable eruptions to the x-axis

-assign the variable waiting to the y-axis

-assign the colour darkorange to all the points

ggplot(faithful) + 
  geom_point(aes(x = eruptions, y = waiting),
             colour = 'darkorange')

Question 3

-Create a plot with the faithful dataset

-use geom_histogram() to plot the distribution of waiting time

-assign the variable waiting to the x-axis

ggplot(faithful) + 
  geom_histogram(aes(x = waiting))

Question 4

-Create a plot with the faithful dataset

-add points with geom_point

-assign the variable eruptions to the x-axis

-assign the variable waiting to the y-axis

-set the shape of the points to cross -set the point size to 4

-set the point transparency 0.3

ggplot(faithful) + 
  geom_point(aes(x = eruptions, y = waiting), shape = 'cross', size = 4 , alpha = 0.4)

Question 5

-Create a plot with the faithful dataset

-use geom_histogram() to plot the distribution of the eruptions (time)

-fill in the histogram based on whether eruptions are greater than or less than 3.2 minutes

ggplot(faithful) + 
  geom_histogram(aes(x = eruptions, fill = eruptions > 3.2))

Question 6

-Create a plot with the mpg dataset

-add geom_bar() to create a bar chart of the variable manufacturer

ggplot(mpg) +
  geom_bar(aes(x = manufacturer))

Question 7

-change code to count and to plot the variable manufacturer instead of class

mpg_counted <- mpg %>% 
  count(manufacturer, name = 'count')
ggplot(mpg_counted) + 
  geom_bar(aes(x = manufacturer, y = count), stat = 'identity')

Question 8

-change code to plot bar chart of each manufacturer as a percent of total

-change class to manufacturer

ggplot(mpg) + 
  geom_bar(aes(x = class, y = after_stat(100 * count / sum(count))))

Question 9

-Use stat_summary() to add a dot at the median of each group

-color the dot blueviolet

-make the shape of the dot cross

-make the dot size 9

ggplot(mpg) + 
  geom_jitter(aes(x = class, y = hwy), width = 0.2) +
  stat_summary(aes(x = class, y = hwy), geom = "point",
               fun = "median", color = "blueviolet",
               shape = "cross", size = 9)