Import Library

library(tidyverse)
## -- Attaching packages ------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ---------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)

———————————————————————

Homework

repeat the the above box plots and bar graphs with horizontal

axis

plot a line graph of cty vs hwy in mpg dataset

plot line graph of depth vs price for each color in diamonds dataset

add legends to all graphs

———————————————————————

Solution

1.repeat the the below box plots and bar graphs with horizontal axis

box plot - statistics horizontal axis

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot() + coord_flip()+ theme(axis.text.x = element_text(angle = 70, vjust = 0.5, color = "red")) + xlab("class") + ylab("hwy") + ggtitle("Class vs hwy :BOX Plot")

use diamonds data to plot bar graphs horizontal axis

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut))+ coord_flip()+ ggtitle("Cut vs Count :Bar Plot")

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, color = cut))+ coord_flip()+ ggtitle("Cut vs Count :Bar Plot")

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut))+ coord_flip()+ ggtitle("Cut vs Count :Bar Plot")

stacked bar graph

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity))+ coord_flip()+ ggtitle("Cut vs Count :stacked Bar Plot")

position = fill shows its easy to compare proportions

ggplot(data = diamonds) +
  geom_bar(
    mapping = aes(x = cut, fill = clarity),
    position = "fill" 
  )+ coord_flip()+ ggtitle("Cut vs Count :stacked Bar Plot")

ggplot(data = diamonds) +
  geom_bar(
    mapping = aes(x = cut, fill = clarity),
    position = "dodge"
  )+ coord_flip()+ ggtitle("Cut vs Count : stacked Bar Plot")

Plot a line graph of cty vs hwy in mpg dataset

ggplot(data = mpg) +
  geom_line(mapping = aes(x = cty, y = hwy,color = class))+ggtitle("cty vs hwy : Line  Plot")

Plot line graph of depth vs price for each color in diamonds dataset

ggplot(data = diamonds) +
  geom_line(mapping = aes(x = depth, y = price, colour=price)) + ggtitle("depth vs price : line  Plot")