Our youtube channel

Our youtube channel has lots of videos on data visualisation in r.

Visit our youtube channel https://www.youtube.com/c/TechAnswers88

https://www.youtube.com/watch?v=MHbzCs05Luo&lc=UgzZxlF1sJYRHczmdx54AaABAg

Libraries needed

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
library(ggeasy)

Example one when you do not have the frequency but you can get the frequency from the individual records. eg. in the mpg dataset the manufacturer is repeated for each car model. so simply count the number of records which had audi in it will give you the total frequency for audi.

mpg

Create the first chart

pl <- ggplot(data = mpg, aes(x = manufacturer, y = after_stat(count/sum(count))))
pl <- pl + geom_bar()
pl <- pl + theme_classic()
pl <-  pl + scale_y_continuous(labels = percent)
pl

Example 2 when you already have the frequency table given to you.

Let us create a dataset first

df <- ggplot2::mpg%>%
      dplyr::group_by(manufacturer)%>%
      tally()

df

Transform the data by adding a percentage column in it

df <- df%>%
      dplyr::mutate(perc = n/sum(n))

df

Create the second chart

pl <- ggplot(data = df, aes(x = manufacturer, y = perc))
pl <- pl + geom_bar(stat = "identity", fill ="orange")
pl <- pl + theme_classic()
pl <- pl + labs(title = "Car Brands")
pl <- pl + labs(subtitle = "Showing Class of cars for each brand")
pl <- pl + labs(caption = paste("Total cars", sum(df$n)))
pl <- pl + labs(x = "Car Brand", y = "Percent")
pl <- pl + ggeasy::easy_rotate_x_labels(angle = 90)
pl <-  pl + scale_y_continuous(labels = percent)

pl