Chapter 4 Methods_hw1

  • Kyoungmi Kim
  • 2020.9.29

4.1 Problem 1

library(ggplot2)
ggplot(data = mpg, aes(x = hwy, fill = drv)) + geom_histogram(alpha = 0.5) +
  theme_minimal() +
  labs(subtitle = "Histogram of Highway Mile Per Gallon",
       y = "count",
       x = "hwy",
       title = "Histogram",
       caption = "source: mpg")

4.2 Problem 2

ggplot(data = mpg, mapping = aes(x = hwy, fill = drv)) + 
  geom_histogram(alpha = 0.5) +
  facet_grid(drv ~ .) +
  theme_minimal() +
  labs(subtitle = "Histogram of Highway Mile Per Gallon",
       y = "count",
       x = "hwy",
       title = "Histogram using facet_grid()",
       caption = "source: mpg")

4.3 Problem 3

ggplot(data = midwest, mapping = aes(x = area, y = poptotal)) +
  coord_cartesian(xlim = c(0, 0.1), ylim = c(0, 500000)) +
  geom_point(alpha = 0.4, mapping = aes(color = state, size = popdensity)) +
  geom_smooth(method = "loess", formula = 'y ~x', se = FALSE) + xlim(c(0, 0.1)) + ylim(c(0, 500000)) +
  theme_classic() +
  labs(subtitle = "Area Vs Population",
       y = "population", options(scipen = 999),
       x = "Area",
       title = "Scatterplot",
       caption = "source: midwest") 

4.4 Problem 4

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

4.5 Problem 5

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

4.6 Problem 6

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