Youtube Channel: https://www.youtube.com/c/TechAnswers88
Simple method to create a battery chart.
Video link https://youtu.be/FLtqThz_O50
library(dplyr) # to manipulate our data
library(ggplot2) # to actually plot our charts
We will create some fictitious data for countries, with their population and the vaccinated population.
# Create our dataset
df <- data.frame(country = c('A','B','C','D','E'),
popMil = c(200,158, 3000, 354, 453),
vaccinatedMil = c(80, 25,2780, 145, 300)
)
We will add a column called max whose value will be 100. We will create a column called perc which will show the percentage of the population who has been vaccinated.
# Transform our dataset
# Create two more columns
df2 <- df%>%
dplyr::mutate(max = 100
,perc = (vaccinatedMil/popMil)* 100)
df2
pl1 <- ggplot(df2, aes(x = country))
pl1 <- pl1 + geom_bar(aes(y = max) , stat= "identity" , fill = "grey")
pl1 <- pl1 + geom_bar(aes(y = perc) , stat= "identity" , fill = "green")
pl1 <- pl1 + theme_minimal()
pl1 <- pl1 + labs(x = "Countries", y = "% of population vaccinated")
pl1 <- pl1 + labs(title= "Vaccination status")
pl1 <- pl1 + labs(subtitle= "Battery Chart 1")
pl1
In this chart we will use geom_text to show the data labels as well.
pl2 <- ggplot(df2, aes(x = country))
pl2 <- pl2 + geom_bar(aes(y = max) , stat= "identity" , fill = "red" , colour = "black")
pl2 <- pl2 + geom_bar(aes(y = perc) , stat= "identity" , fill = "blue" , colour ="blue")
pl2 <- pl2 + geom_text (aes(y = perc, label =paste0(round(perc,1) , "%") )
, vjust = -0.5
, color = "white"
, size = 2.5)
pl2 <- pl2 + theme_minimal()
pl2 <- pl2 + labs(x = "Countries", y = "% of population vaccinated")
pl2 <- pl2 + labs(title= "Vaccination status")
pl2 <- pl2 + labs(subtitle= "Battery Chart 2")
pl2
In this example we will put a small cap similar to a battery.
# Put a battery cap on the top
pl3 <- ggplot(df2, aes(x = country))
pl3 <- pl3 + geom_bar(aes(y = 101) , stat= "identity" , fill ="black" , width = 0.5) # puts the cap on the battery
pl3 <- pl3 + geom_bar(aes(y = max) , stat= "identity" , fill ="black" , colour = "black")
pl3 <- pl3 + geom_bar(aes(y = perc) , stat= "identity" , fill = "green" , colour ="black")
pl3 <- pl3 + geom_text (aes(y = perc, label =paste0(round(perc,1) , "%") ) , vjust = -0.5 , color = "white", size = 2.5)
pl3 <- pl3 + theme_minimal() + theme(panel.grid = element_blank())
pl3 <- pl3 + labs(x = "Countries", y = "% of population vaccinated")
pl3 <- pl3 + labs(title= "Vaccination status")
pl3 <- pl3 + labs(subtitle= "Battery Chart 3")
pl3
We will sort the order of the bars based on the perc column using the reorder command. reorder(country, - perc)
# Put a battery cap on the top
pl4 <- ggplot(df2, aes(x = reorder(country, - perc))) # arranging the bar in descending order of perc
pl4 <- pl4 + geom_bar(aes(y = 101) , stat= "identity" , fill ="black", width = 0.5) # puts the cap on the battery
pl4 <- pl4 + geom_bar(aes(y = max) , stat= "identity" , fill ="grey", colour = "black")
pl4 <- pl4 + geom_bar(aes(y = perc) , stat= "identity" , fill = "green", colour ="black")
pl4 <- pl4 + geom_text (aes(y = perc, label =paste0(round(perc,1) , "%") ) , vjust = -0.5 , color = "white", size = 2.5)
pl4 <- pl4 + theme_minimal() + theme(panel.grid = element_blank())
pl4 <- pl4 + labs(x = "Countries", y = "% of population vaccinated")
pl4 <- pl4 + labs(title= "Vaccination status")
pl4 <- pl4 + labs(subtitle= "Battery Chart 4")
pl4