youtube video link with explanations for these examples https://youtu.be/RPwJ6ExwPbg

Following packages are being used in this example

library(ggplot2)
library(ggthemes)
library(scales)
library(dplyr)

First basic plot

# We will use  a dataset which comes with the package ggplot.
# view the data frame to see the structure
head(ggplot2::mpg)
pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar(stat="count")
pl

Note that we have used fill =“class”. class is a data field in the mpg dataset. We have rotated the x axis text to 90 using the angle = 90

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count")
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl

Creating a 100% stacked bar chart using the position =“fill” option in the geom_bar.

Adding more bells and whistles to our chart by adding a theme, titles, captions.

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count", position ="fill")
pl <- pl + ggthemes::theme_economist()
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Percentage")
pl <- pl + scale_y_continuous(labels = scales::percent)
#pl <- pl + ggplot2::coord_flip()
pl

Stacked Barchart Using a aggregated data set from mpg Created additional field to get the percentage values Then used the percentage values in geom_text

head(ggplot2::mpg)
dt <- mpg%>%
  dplyr::group_by(manufacturer, class)%>%
  dplyr::tally()%>%
  dplyr::mutate(percent=n/sum(n))


pl <- ggplot(data = dt,aes(x= manufacturer, y = n,fill = class))
pl <- pl + geom_bar(stat="identity")
pl <- pl + geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
                     position=position_stack(vjust=0.5), colour="white", size = 2)


pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl

100% Stacked Barchart by adding the position =“fill” in geom_bar

pl <- ggplot(data = dt,aes(x= manufacturer, y = n,fill = class))
pl <- pl + geom_bar(stat="identity", position ="fill")
pl <- pl + geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
                     position=position_fill(vjust=0.5), colour="white", size =2)

pl <- pl + theme_minimal()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Percentage")
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl

Flipped the axis to get a horizontal 100% stacked bar chart

pl <- ggplot(data = dt,aes(x= manufacturer, y = n,fill = class))
pl <- pl + geom_bar(stat="identity", position ="fill")
pl <- pl + geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
                     position=position_fill(vjust=0.5), colour="white")

pl <- pl + theme_minimal()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Percentage")
pl <- pl + coord_flip()
pl

#techanswers88