Chapter6 Problem 6

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

  • Set width = 0.5 for the width of bars
  • Rotate tick labels in the x-axis by 65 degree
  • Use palette = “Spectral” for color

Answer

library(ggplot2)
library(RColorBrewer)

ggplot(mpg, aes(x = manufacturer, fill = class)) +
  geom_bar(width=0.5) +
  labs(title = "Barplot", 
       subtitle = "Manufacture across Vehicle Classes", 
       x = "manufacture" 
  ) + 
  scale_fill_brewer(palette = "Spectral") + 
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 65))