Chapter 4 Methods

Load ggplot2

library("ggplot2")

Problem 1

ggplot(data = mpg, aes(x = hwy)) + 
  geom_histogram(alpha = 0.5, aes(fill = drv)) +
  theme_minimal() + 
  labs(
     subtitle = "Histogram of Highway Mile Per Gallon", 
     title = "Histogram", 
     caption = "Source: mpg"
  )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Problem 2

ggplot(data = mpg, aes(x = hwy)) + 
  geom_histogram(alpha = 0.5, aes(fill = drv)) +
  facet_grid(rows = vars(drv)) + 
  theme_minimal() + 
  labs(
     subtitle = "Histogram of Highway Mile Per Gallon", 
     title = "Historam usig facet_grid()", 
     caption = "Source: mpg"
  )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Problem 3

options(scipen=999)
ggplot(data = midwest, aes(x = area, y = poptotal)) +
  geom_point(alpha = 0.4, aes(color = state, size = popdensity)) +
  geom_smooth(se = FALSE) +
  theme_classic() + 
  labs(
     subtitle = "Area Vs Population", 
     y = "Population", 
     x = "Area", 
     title = "Scatter Plot", 
     caption = "Source: midwest"
  ) + 
  scale_x_continuous(limits = c(0, 0.1)) +
  scale_y_continuous(limits = c(0, 500000))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Problem 4

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(alpha = 0.5, size = 6, aes(shape = Species, color = Species)) + 
  theme_minimal() + 
  labs(
     subtitle = "Sepal.Length Vs Sepal.Width", 
     title = "Scatter Plot", 
     caption = "Source: iris"
  )

Problem 5

library("gcookbook")
ggplot(data = heightweight, aes(x = heightIn, y = weightLb)) +
  geom_point(size = 3, alpha = 0.5, aes(color = sex)) + 
  geom_smooth(aes(color = sex, x = heightIn, y = weightLb), method = "lm", se = FALSE) +
  theme_classic() + 
    labs(
     subtitle = "Weight Vs Height", 
     title = "Scatter Plot", 
     caption = "Source: heightweight"
  )
## `geom_smooth()` using formula 'y ~ x'

Problem 6

ggplot(data = mpg, aes(x = manufacturer)) + 
  geom_bar(width = 0.5, aes(fill = class)) + 
  scale_fill_brewer(palette = "Spectral") + 
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 65, hjust = 1)) +  
  labs(
     subtitle = "Manufacturer across Vehicle Classes", 
     title = "Bar Plot"
  )