QURTO

Animate a circular packing plot showing district-wise population trends.

##Introduction

Data visualization helps to represent complex data in a simple and understandable way.

Circular packing is a technique where data is shown using circles, and the size of each circle represents the value.

Animation helps to show how data changes over time.

Objective

To visualize district-wise population data To represent population using circular packing To analyze population differences across districts To show changes over different years To create an animated visualization Problem Explanation

The dataset does not contain population or year information.

To solve this problem:

Region column is used as District Population values are simulated for visualization Year values are randomly assigned Circular packing is used to represent population Animation is used to show changes over time

This helps in understanding population distribution across districts.

STEP 1: Load the Dataset

data <- read.csv("C:/Users/Vaishnavi/Downloads/archive/Global_Superstore2.csv")
head(data)
  Row.ID        Order.ID Order.Date  Ship.Date    Ship.Mode Customer.ID
1  32298  CA-2012-124891 31-07-2012 31-07-2012     Same Day    RH-19495
2  26341   IN-2013-77878 05-02-2013 07-02-2013 Second Class    JR-16210
3  25330   IN-2013-71249 17-10-2013 18-10-2013  First Class    CR-12730
4  13524 ES-2013-1579342 28-01-2013 30-01-2013  First Class    KM-16375
5  47221    SG-2013-4320 05-11-2013 06-11-2013     Same Day     RH-9495
6  22732   IN-2013-42360 28-06-2013 01-07-2013 Second Class    JM-15655
     Customer.Name     Segment          City           State       Country
1      Rick Hansen    Consumer New York City        New York United States
2    Justin Ritter   Corporate    Wollongong New South Wales     Australia
3     Craig Reiter    Consumer      Brisbane      Queensland     Australia
4 Katherine Murray Home Office        Berlin          Berlin       Germany
5      Rick Hansen    Consumer         Dakar           Dakar       Senegal
6      Jim Mitchum   Corporate        Sydney New South Wales     Australia
  Postal.Code Market  Region       Product.ID   Category Sub.Category
1       10024     US    East  TEC-AC-10003033 Technology  Accessories
2          NA   APAC Oceania  FUR-CH-10003950  Furniture       Chairs
3          NA   APAC Oceania  TEC-PH-10004664 Technology       Phones
4          NA     EU Central  TEC-PH-10004583 Technology       Phones
5          NA Africa  Africa TEC-SHA-10000501 Technology      Copiers
6          NA   APAC Oceania  TEC-PH-10000030 Technology       Phones
                                                        Product.Name    Sales
1 Plantronics CS510 - Over-the-Head monaural Wireless Headset System 2309.650
2                          Novimex Executive Leather Armchair, Black 3709.395
3                                  Nokia Smart Phone, with Caller ID 5175.171
4                                     Motorola Smart Phone, Cordless 2892.510
5                                     Sharp Wireless Fax, High-Speed 2832.960
6                                Samsung Smart Phone, with Caller ID 2862.675
  Quantity Discount    Profit Shipping.Cost Order.Priority
1        7      0.0  762.1845        933.57       Critical
2        9      0.1 -288.7650        923.63       Critical
3        9      0.1  919.9710        915.49         Medium
4        5      0.1  -96.5400        910.16         Medium
5        8      0.0  311.5200        903.04       Critical
6        5      0.1  763.2750        897.35       Critical

Explanation: Loads the dataset and displays first rows.


STEP 2: Load Libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(packcircles)
Warning: package 'packcircles' was built under R version 4.5.3
library(ggplot2)
library(gganimate)
Warning: package 'gganimate' was built under R version 4.5.3
library(gifski)
Warning: package 'gifski' was built under R version 4.5.3

Explanation: Used for data manipulation, circular packing, plotting, and animation.


STEP 3: Prepare Data

set.seed(1)

# Use Region as District
data$District <- data$Region

# Simulate Year and Population
data$Year <- sample(2018:2020, nrow(data), replace = TRUE)
data$Population <- sample(10000:100000, nrow(data), replace = TRUE)

df <- data %>%
  group_by(District, Year) %>%
  summarise(Population = sum(Population), .groups = "drop")

Explanation: Since the dataset does not contain population data, values are simulated for demonstration.


STEP 4: Circular Packing Function

create_fun <- function(x){
  layout <- circleProgressiveLayout(x$Population, sizetype = "area")
  x <- cbind(x, layout)
  vertices <- circleLayoutVertices(layout)
  list(data = x, vertices = vertices)
}

Explanation: Creates circle positions and shapes.


STEP 5: Prepare Animation Data

split_data <- df %>% arrange(Year) %>% group_split(Year)

years <- sort(unique(df$Year))

frames <- lapply(split_data, create_fun)

plot_data <- purrr::map2_df(frames, years,
                           ~mutate(.x$vertices, Year = .y))

Explanation: Prepares data for animation.


STEP 6: Static Plot (Single Year)

single_year <- df %>% filter(Year == min(Year))
circle_data <- create_fun(single_year)

ggplot(circle_data$vertices, aes(x, y, group = id, fill = as.factor(id))) +
  geom_polygon(color = "black") +
  coord_equal() +
  theme_void() +
  scale_fill_viridis_d() +
  labs(title = "Circular Packing (Single Year)")

Explanation: Shows circular packing for one year.


STEP 7: Animation Plot

p <- ggplot(plot_data, aes(x, y, group = id, fill = as.factor(id))) +
  geom_polygon(color = "black", alpha = 0.8) +
  coord_equal() +
  theme_void() +
  scale_fill_viridis_d() +   
  labs(title = "Year: {frame_time}") +
  transition_time(Year)

anim <- animate(p, duration = 6, fps = 5, renderer = gifski_renderer())
anim

Explanation: Creates animated circular packing plot over years.


Interpretation

  • Larger circles represent higher (simulated) population
  • Static plot shows one snapshot
  • Animation shows changes over time

Visualization

An animated circular packing plot is used to visualize district-wise population trends.

Each circle represents a district Circle size represents population value Different colors represent different districts Each frame represents a different year Animation shows changes over time Helps compare districts easily

Conclusion

Circular packing effectively represents population distribution Larger circles indicate higher population Animation helps visualize changes across years This method makes complex data easy to understand Useful for comparing district-wise trends Thank You