la1 project

Author

srushtivh(218) spandanavr(215)

Design a waffle chart representing distribution of occupations in urban vs rural areas.

Step1:Define the data in rstudio

#load necessary packages
library(tibble)
#define the occupation distribution data
Occupation_data <-tribble(~Occupation,~urban,~rural,
                         "Agriculture",5,30,
                         "manufacturing",20,25,
                         "Services",40,20,
                         "Education/HealthCare",25,15,
                         "Other",10,10
                         )
print(Occupation_data)
# A tibble: 5 × 3
  Occupation           urban rural
  <chr>                <dbl> <dbl>
1 Agriculture              5    30
2 manufacturing           20    25
3 Services                40    20
4 Education/HealthCare    25    15
5 Other                   10    10

what this does:

  • 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 data
Occupation_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 format
Occupation_long<-pivot_longer(Occupation_data,cols = c(Urban,Rural),
                              names_to = "Area",values_to = "Percentage")
#create waffle chart
ggplot(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()