Problem 1

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

Answer 1

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

Problem 2

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

Answer 2

library(ggplot2)

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

Problem 3

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

Answer 3

library(ggplot2)
options(scipen=999)

#ggplot(midwest, aes(x = area, y = poptotal, color = state, size = popdensity, group = PID)) +
ggplot(midwest, aes(x = area, y = poptotal, color = state, size = popdensity)) +
  geom_point(alpha = 0.4) +
  scale_x_continuous(limits = c(0, 0.1)) +
  scale_y_continuous(limits = c(0, 500000)) +
  geom_smooth(aes(group = 1), se=FALSE, show.legend=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).

Problem 4

Using the iris dataset in the datasets package (dataset package belongs to Base R and so you don’t need to download the package), replicate the plot below using the following settings:

(iris is another famous dataset in R. You may google or check the this link to learn more about the dataset)

Answer 4

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

Problem 5

Using the heightweight dataset in the gcookbook package, replicate the plot below using the following settings:

Answer 5

library(gcookbook)

ggplot(heightweight, aes(x = heightIn, y = weightLb, color = sex)) +
  geom_point(alpha = 0.5, size=3) +
#  scale_x_continuous(limits = c(0, 0.1)) +
#  scale_y_continuous(limits = c(0, 500000), labels=scaleFUN) +
  geom_smooth(method = "lm", se=FALSE) +
  labs(title = "Scatterplot", 
       subtitle = "Weight Vs Height", 
       caption = "source: heightweight"
#       x = "Area", 
#       y = "Population"
  ) + 
  theme_classic()  
## `geom_smooth()` using formula 'y ~ x'

Problem 6

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

Answer 6

library(ggplot2)
library(RColorBrewer)

ggplot(mpg, aes(x = manufacturer, fill = class)) +
  geom_bar(width=0.5) +
#  scale_x_continuous(limits = c(0, 0.1)) +
#  scale_y_continuous(limits = c(0, 500000), labels=scaleFUN) +
  labs(title = "Barplot", 
       subtitle = "Manufacture across Vehicle Classes", 
#       caption = "source: heightweight"
       x = "manufacture" 
#       y = "Population"
  ) + 
  scale_fill_brewer(palette = "Spectral") + 
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 65))

`