Chapter3 Problem 3

Using the midwest dataset in the ggplot2 package, replicate the plot below using the following settings:

  • Map x=area and y=poptotal
  • Set alpha = 0.4
  • Set the limit of x-axis is c(0, 0.1)
  • Set the limit of y-axis is c(0, 500000)
  • Use se=FALSE option within geom_smooth() to remove confidence bands
  • Use theme_classic()

Answer

library(ggplot2)
options(scipen=999)

ggplot(midwest, aes(x = area, y = poptotal)) +
  geom_point(aes(color = state, size = popdensity), alpha = 0.4) +
  scale_x_continuous(limits = c(0, 0.1)) +
  scale_y_continuous(limits = c(0, 500000)) +
  geom_smooth(se=FALSE) +
  labs(title = "Scatterplot", 
       subtitle = "Area Vs Population", 
       caption = "source: midwest",
       x = "Area", 
       y = "Population"
  ) + 
  theme_classic()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).