Use the built-in data “mpg” with the ggplot2 package to do the following:

  1. Create a bar graph for the “manufacturer” variable. Names of manufacturers should be displayed at a 90 degree angle.
ggplot(mpg, aes(x = manufacturer))+
  geom_bar(fill = "antiquewhite4")+
  theme(axis.text.x = element_text(angle = 90))+
  labs(title = "How many Manufacturers form each Dealer", x = "Manufacturer", y = "Frequency" )+
  theme(plot.title = element_text(size = 25))

  1. Create a bar graph for the “year” variable.
M = mpg[,4]

barplot(table(M), xlab = NULL, ylab = "count", col = "aliceblue") +
  title("Number of different cars in 1999 and 2008 from mpg dataset", font.main = 2)

## numeric(0)
  1. Create a separate density curve for each of the 3 quantitative variables (displ, cty, hwy), conditioning on each type of cylinder(4 levels). You should overlay the 4 curves for displ, 4 for cty, and 4 for hwy. You should have 3 separate plots, each having 4 curves.
ggplot(mpg, aes(x = displ , color = as.factor(cyl))) +
  geom_density()+
  labs(title = "Density of Engine Displacement", x = "Engine Displacement", y = "Density", color = "Cylinder")

ggplot(mpg, aes(x = cty, color = as.factor(cyl))) +
  geom_density()+
  labs(title = "Density of City mpg", x = "mpg in city", y = "Density",   color = "Cylinder")

ggplot(mpg , aes(x = hwy, color = as.factor(cyl))) +
  geom_density()+
  labs(title = "Density of Highway mpg", x = "mpg on Highway", y = "Density", color = "Cylinder")

  1. Create side-by-side boxplots for displ by cyl.
ggplot(mpg, aes(x = as.factor(cyl), y = displ))+ 
  geom_boxplot()+
  labs(title = "Engine Displacement vs Cylinder", x = "Cylinder", y = "Engine Displacement")+
  theme(plot.title = element_text(size = 30))