Questions

  1. Stacked bar chart on the mpg dataset
#Intsall ggplot package
require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.1
#View data
View(mpg)
#stacked bar with tarnsmission type for each vehicle class
ggplot(data = mpg) +
  geom_bar(mapping = aes(x = class, fill = trans)) + scale_fill_discrete(name="transmission")

  1. Box plot on the mpg dataset
#Flip x and y axes using coord_flip
ggplot(data = mpg, mapping = aes(x = manufacturer, y = hwy)) +
  geom_boxplot() +
  coord_flip() +labs(x = "Vehicle Manufacturer", y = "Highway Fuel Efficiency (miles/gallon)")

  1. Use ggthemes on diamonds dataset
require(ggthemes)
## Loading required package: ggthemes
View(diamonds)
ggplot(data = diamonds, aes(x = price, fill = cut)) + 
  geom_density(alpha = 0.2) +
  ylab("Density") + 
  xlab("Diamond Price (USD)") + 
  ggtitle("Diamond Price Density") + theme(legend.position="top")

  1. Scatter plot showing the relationship between sepal length and petal legth on iris dataset
View(iris)
# Add the regression line
ggplot(data = iris, aes(x=Sepal.Length, y=Petal.Length)) + 
  geom_point()+
  geom_smooth(method=lm) + ylab("Iris Petal Length") + 
  xlab("Iris Sepal Length") + 
  ggtitle("Relationship Between Petal and Sepal Length")

  1. Scatter plot showing the relationship between sepal length and petal legth on iris dataset using species level comparison
ggplot(data = iris, aes(x=Sepal.Length, y=Petal.Length, color = Species)) + 
  geom_point()+
  geom_smooth(method=lm, se=FALSE) + ylab("Iris Petal Length") + 
  xlab("Iris Sepal Length") + 
  ggtitle("Relationship Between Petal and Sepal Length", subtitle = "Sepal level comparison") + theme(legend.position="bottom") + theme_classic()