Our youtube channel has lots of videos on data visualisation in r.
Visit our youtube channel https://www.youtube.com/c/TechAnswers88
##Video link for watching this example https://youtu.be/152i9oCb6Ds
# We will need the following packages (install them using the above code if needed)
library(ggplot2)
# data to be used for the chart
# We will following packages to manage our data
library(reshape2)
library(dplyr)
d <- read.csv("https://covid.ourworldindata.org/data/ecdc/total_cases.csv")
d <- data.frame(d)
#View(d)
dMelt <- melt(d, id ="date")
#View(dMelt)
dToday <- dMelt%>%
dplyr::filter(!variable %in% 'World')%>%
dplyr::filter(date %in% max(date))
str(dToday)
FALSE 'data.frame': 214 obs. of 3 variables:
FALSE $ date : chr "2020-11-29" "2020-11-29" "2020-11-29" "2020-11-29" ...
FALSE $ variable: Factor w/ 215 levels "World","Afghanistan",..: 2 3 4 5 6 7 8 9 10 11 ...
FALSE $ value : int 45844 36790 81212 6670 15087 4 141 1413362 134768 4833 ...
#When to use the treemap
# good alternative to a pie chart
Top20 <- dToday%>%dplyr::arrange(-value)%>%head(20)
pl <- ggplot(data = Top20, aes(x= variable,y = value))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl
pl <- ggplot(data = Top20, aes(x= variable,y = value))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar()
pl
pl <- ggplot(data = Top20, aes(x= variable,y = value))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl
pl <- ggplot(data = Top20, aes(x= '',y = value))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl
#install.packages("randomcoloR")
library(randomcoloR)
n <- 20
palette <- distinctColorPalette(n)
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + geom_text(aes(label = paste0(round(value/sum(value)*100), "%")), position = position_stack(vjust = 0.5))
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl
pl <- ggplot(data = Top20, aes(x= '',y = value, fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + geom_text(aes(label = paste0(round(value/sum(value)*100), "%")), position = position_stack(vjust = 0.5))
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl <- pl + labs(title ="Pie chart of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + labs(caption ="#technanswers88 on youtube")
pl
pl <- ggplot(data = Top20, aes(x= '',y = reorder(value,value), fill = variable))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + geom_text(aes(label = paste0(round(value/sum(value)*100), "%")), position = position_stack(vjust = 0.5))
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "none")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl <- pl + labs(title ="Pie chart of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + labs(caption ="#technanswers88 on youtube")
pl
Top20WithMyLabels <- Top20%>%
dplyr::mutate(id = LETTERS[row_number()] )
pl <- ggplot(data = Top20WithMyLabels, aes(x= '',y = value, fill = paste0(id,' : ',variable,'(', round(value/sum(value)*100),'%)' )))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + geom_text(aes(x = 1.4,label = id ), position = position_stack(vjust = 0.5))
pl <- pl + theme_void()
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "top")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl <- pl + labs(title ="Pie chart of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + labs(caption ="#technanswers88 on youtube")
pl
If for some reason you are not able to download the data from the weblink then use the following code which will generate the data locally.
CreatedDataWithLabels <- data.frame(date = c('29/11/2020', '29/11/2020', '29/11/2020'
,'29/11/2020', '29/11/2020', '29/11/2020'
,'29/11/2020','29/11/2020', '29/11/2020'
,'29/11/2020', '29/11/2020','29/11/2020'
,'29/11/2020', '29/11/2020','29/11/2020'
,'29/11/2020', '29/11/2020','29/11/2020'
,'29/11/2020','29/11/2020')
, variable = c('United.States','India','Brazil','Russia', 'France'
,'United.Kingdom','Italy', 'Argentina'
,'Colombia','Mexico','Germany','Poland'
,'Peru','Iran','South.Africa', 'Ukraine'
,'Chile','Iraq','Indonesia', 'Czech.Republic')
, value = c(13246651 ,9392919,6290272,2269316,2208699,1605172,1564532
,1413362,1299613,1100683,1042700,973593,960368,935799,785139,693407
,548941,548821,527999,518649)
, id = c('A','B','C','D','E','F','G','H'
,'I','J','K','L','M','N','O','P','Q','R','S','T')
)
pl <- ggplot(data = CreatedDataWithLabels, aes(x= '',y = value, fill = paste0(id,' : ',variable,'(', round(value/sum(value)*100),'%)' )))
pl <- pl + geom_bar(width = 1, stat = "identity")
pl <- pl + geom_text(aes(x = 1.4,label = id ), position = position_stack(vjust = 0.5))
pl <- pl + theme_void()
pl <- pl + theme_classic()
pl <- pl + theme(legend.position = "top")
pl <- pl + coord_polar("y", start=0)
pl <- pl + scale_fill_manual(values = palette)
pl <- pl + theme(axis.line = element_blank())
pl <- pl + theme(axis.text = element_blank())
pl <- pl + theme(axis.ticks = element_blank())
pl <- pl + labs(x = NULL, y = NULL, fill = NULL)
pl <- pl + labs(title ="Pie chart of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + labs(caption ="#technanswers88 on youtube")
pl