# Load required libraries
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data("USArrests")
# Extract values from the second column
assault_arrests <- USArrests[, 2]
# Create a data frame with state names and corresponding assault arrests
state_data <- data.frame(State = row.names(USArrests), AssaultArrests = assault_arrests)
# Sort the data frame by the frequency of assault arrests in descending order
state_data <- state_data[order(-state_data$AssaultArrests), ]
# Create a pie chart using plotly
pie_chart <- plot_ly(labels = state_data$State, values = state_data$AssaultArrests, type = "pie",
marker = list(colors = rainbow(length(state_data$State))),
textinfo = "percent+label", textposition = "inside",
hole = 0.4, title = "Pie Chart of Assault Arrests by State")
# Set layout options to avoid overlap
layout_options <- list(
title = "Pie Chart of Assault Arrests by State",
showlegend = FALSE,
annotations = list(
x = 0.5, y = 0.5, font = list(size = 10),
text = paste("Total Arrests:", sum(state_data$AssaultArrests)),
showarrow = FALSE
)
)
# Display the chart
pie_chart <- layout(pie_chart, layout_options)
pie_chart