Design a waffle chart representing distribution of occupations in urban vs rural areas.
Step1:Define the data in rstudio
#load necessary packageslibrary(tibble)#define the occupation distribution dataOccupation_data <-tribble(~Occupation,~urban,~rural,"Agriculture",5,30,"manufacturing",20,25,"Services",40,20,"Education/HealthCare",25,15,"Other",10,10 )print(Occupation_data)
tribble() is an easy way to define small tables manually.
we are creating a table with three columns:
-Occupation (job category)
-Urban(percentage of urban population in this occupation)
-Rural(percentage of rural population in this occupation)
Step2:Install required packages
Step3:R code to create waffle chart
library(waffle)
Loading required package: ggplot2
library(ggplot2)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(tidyr)#create dataOccupation_data<-data.frame(Occupation =c("Agriculture","Manufacturing","Services","Education/Healthcare","Other"),Urban=c(5,20,40,25,10),Rural=c(30,25,20,15,10))#convert to long formatOccupation_long<-pivot_longer(Occupation_data,cols =c(Urban,Rural),names_to ="Area",values_to ="Percentage")#create waffle chartggplot(Occupation_long,aes(fill = Occupation,values = Percentage))+geom_waffle(n_rows =10,size =0.33,color ="white")+facet_wrap(~Area,scales="free",ncol=2)+scale_fill_manual(values =c("red","blue","green","gold","purple"))+labs(title ="Occupation distribution in urban vs rural areas",caption="Each square =1% of total ")+theme_minimal()