library(sunburstR)
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)
library(readr)
library(htmlwidgets)
library(htmltools)
data <- read_csv("~/Downloads/DATA VISUALIZATION/Final Project/master Data/All India level Person Arrested under Cyber Crime (IT Act + IPC Section) by Age Group during 2002-2011.csv")
## Rows: 176 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Category, Crime Head
## dbl (7): Below 18 Years, Between 18 - 30 Years, Between 30 -45 Years, Betwee...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data_sunburst <- data %>%
filter(Category != "Grand Total (Offences under IT Act + IPC)") %>%
select(Category, `Crime Head`, Year,
`Below 18 Years`, `Between 18 - 30 Years`,
`Between 30 -45 Years`, `Between 45 - 60 Years`,
`Above 60 Years`) %>%
pivot_longer(
cols = c(`Below 18 Years`, `Between 18 - 30 Years`,
`Between 30 -45 Years`, `Between 45 - 60 Years`,
`Above 60 Years`),
names_to = "Age_Group",
values_to = "Total_Arrested"
) %>%
filter(Total_Arrested > 0) %>%
mutate(
Category = ifelse(is.na(Category), "Unknown", Category),
`Crime Head` = ifelse(is.na(`Crime Head`), "Unknown", `Crime Head`),
Year = ifelse(is.na(Year), "Unknown", Year),
Age_Group = ifelse(is.na(Age_Group), "Unknown", Age_Group),
path = paste(Category, `Crime Head`, Year, Age_Group, sep = "-")
) %>%
select(path, total = Total_Arrested, Age_Group)
head(data_sunburst) # Verify data
## # A tibble: 6 × 3
## path total Age_Group
## <chr> <dbl> <chr>
## 1 Offences under IT Act-Tampering Computer Source Department (S… 4 Between …
## 2 Offences under IT Act-Tampering Computer Source Department (S… 3 Between …
## 3 Offences under IT Act-Hacking Computer Systems - i) Loss/ Dam… 4 Between …
## 4 Offences under IT Act-Hacking Computer Systems - ii) Hacking … 28 Between …
## 5 Offences under IT Act-Hacking Computer Systems - ii) Hacking … 5 Between …
## 6 Offences under IT Act-Hacking Computer Systems - ii) Hacking … 1 Between …
age_colors <- c(
"Below 18 Years" = "#1b9e77",
"Between 18 - 30 Years" = "#d95f02",
"Between 30 -45 Years" = "#7570b3",
"Between 45 - 60 Years" = "#e7298a",
"Above 60 Years" = "#66a61e"
)
sunburst(data_sunburst,
count = TRUE,
percent = TRUE,
colors = age_colors,
legend = list(w = 150, h = 20, r = 10),
legendOrder = names(age_colors))
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
# Remove 'Grand Total' and structure data correctly for each year
data_sunburst <- data %>%
filter(Category != "Grand Total (Offences under IT Act + IPC)") %>%
select(Category, `Crime Head`, Year,
`Below 18 Years`, `Between 18 - 30 Years`,
`Between 30 -45 Years`, `Between 45 - 60 Years`,
`Above 60 Years`) %>%
pivot_longer(
cols = c(`Below 18 Years`, `Between 18 - 30 Years`,
`Between 30 -45 Years`, `Between 45 - 60 Years`,
`Above 60 Years`),
names_to = "Age_Group",
values_to = "Total_Arrested"
) %>%
filter(Total_Arrested > 0) %>%
mutate(
path = paste(Category, `Crime Head`, Age_Group, sep = "-") # Exclude Year from path to filter dynamically
) %>%
select(path, total = Total_Arrested, Year, Age_Group)
head(data_sunburst) # Check data
## # A tibble: 6 × 4
## path total Year Age_Group
## <chr> <dbl> <dbl> <chr>
## 1 Offences under IT Act-Tampering Computer Source Departm… 4 2002 Between …
## 2 Offences under IT Act-Tampering Computer Source Departm… 3 2002 Between …
## 3 Offences under IT Act-Hacking Computer Systems - i) Los… 4 2002 Between …
## 4 Offences under IT Act-Hacking Computer Systems - ii) Ha… 28 2002 Between …
## 5 Offences under IT Act-Hacking Computer Systems - ii) Ha… 5 2002 Between …
## 6 Offences under IT Act-Hacking Computer Systems - ii) Ha… 1 2002 Between …
age_colors <- c(
"Below 18 Years" = "#1b9e77",
"Between 18 - 30 Years" = "#d95f02",
"Between 30 -45 Years" = "#7570b3",
"Between 45 - 60 Years" = "#e7298a",
"Above 60 Years" = "#66a61e"
)
unique_years <- unique(data_sunburst$Year)
for (year_selected in unique_years) {
# Filter data for the specific year
data_filtered <- data_sunburst %>%
filter(Year == year_selected)
# Create Sunburst Chart
chart <- sunburst(data_filtered,
count = TRUE,
percent = TRUE,
colors = age_colors,
legend = list(w = 150, h = 20, r = 10),
legendOrder = names(age_colors))
# Add a title with the year
chart <- htmlwidgets::prependContent(chart, htmltools::tags$h2(paste("Sunburst Chart - Year:", year_selected)))
print(chart)
}
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.