-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathGroup6-Final-1 (2).R
More file actions
More file actions
Latest commit
112 lines (97 loc) · 4.75 KB
/
Group6-Final-1 (2).R
File metadata and controls
112 lines (97 loc) · 4.75 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Load libraries
library(tidyverse)
library(ggplot2)
library(plotly)
library(reshape2)
library(dplyr)
library(readr)
library(tidyr)
# Load dataset
df <- read.csv("human-rights-index-vdem-cleaned.csv")
# Create an interactive histogram using Plotly to show the distribution of Civil Liberties Index
fig_hist <- plot_ly(df, x = ~Civil_Liberties_Index, type = "histogram", nbinsx = 30,
marker = list(color = "blue", line = list(color = "black", width = 1)))
# Customize layout for better readability
fig_hist <- fig_hist %>% layout(title = "Distribution of Civil Liberties Index",
xaxis = list(title = "Civil Liberties Index"),
yaxis = list(title = "Frequency"),
bargap = 0.1)
fig_hist
# Line plot - Civil Liberties Trends for Selected Countries
selected_countries <- c("United States", "China", "India", "Germany", "Brazil", "South Africa")
df_filtered <- df %>% filter(Entity %in% selected_countries) # Filter data for selected countries
# Create line plot showing trends over time
ggplot(df_filtered, aes(x = Year, y = Civil_Liberties_Index, color = Entity)) +
geom_line(size = 1) +
ggtitle("Trend of Civil Liberties Index Over Time") +
xlab("Year") +
ylab("Civil Liberties Index") +
theme_minimal()
# Bar plot: Civil Liberties Index by Country and Region
# Assign regions to selected countries
df_region_bar <- df %>%
filter(Entity %in% selected_countries) %>%
mutate(Region = case_when(
Entity %in% c("United States", "Canada") ~ "North America",
Entity %in% c("Germany", "France", "UK", "Italy") ~ "Europe",
Entity %in% c("India", "China", "Japan", "South Korea") ~ "Asia",
Entity %in% c("Brazil", "Argentina", "Chile") ~ "South America",
Entity %in% c("South Africa", "Nigeria", "Kenya", "Egypt") ~ "Africa",
TRUE ~ "Other"
))
# Create bar plot
ggplot(df_region_bar, aes(x = Entity, y = Civil_Liberties_Index, fill = Region)) +
geom_bar(stat = "identity") +
ggtitle("Civil Liberties Index by Country and Region") +
xlab("Country") +
ylab("Civil Liberties Index") +
theme_minimal()
# Create an interactive heatmap using Plotly
# Convert Year to numeric
df_heatmap <- df # Ensure df_heatmap is correctly defined
df_heatmap$Year <- as.numeric(df_heatmap$Year)
# Generate heatmap (Only Years After 1950)
fig_heat <- plot_ly(df_heatmap, x = ~Year, y = ~Entity, z = ~Civil_Liberties_Index, type = "heatmap", colorscale = "RdYlGn",
showscale = TRUE)
# Customize layout
fig_heat <- fig_heat %>% layout(title = "Interactive Heatmap of Civil Liberties (After 1950)",
xaxis = list(title = "Year", tickmode = "array",
tickvals = seq(1950, max(df_heatmap$Year), by = 5),
tickangle = 45),
yaxis = list(title = "Country"))
fig_heat
# Choropleth Map: Global Civil Liberties Index
latest_year <- max(df$Year, na.rm = TRUE) # Get latest available year
df_latest <- df %>% filter(Year == latest_year) # Filter data for the latest year
# Generate interactive world map
fig <- plot_ly(df_latest, type = "choropleth",
locations = ~Code,
z = ~Civil_Liberties_Index,
text = ~Entity, colorscale = "RdBu",
reversescale = TRUE, colorbar = list(title = "Civil Liberties Index"))
# Customize layout
fig <- fig %>% layout(title = paste("Global Civil Liberties Index -", latest_year),
geo = list(projection = list(type = "equirectangular"),
showcoastlines = TRUE, coastlinecolor = "black",
showland = TRUE, landcolor = "lightgray"))
fig
# Box Plot: Highlight Civil Liberties Index in Key Historical Years
highlight_years <- c(1945, 1989, 2001)
df_highlight <- df %>% filter(Year %in% highlight_years)
fig1 <- plot_ly(df_highlight, x = ~as.factor(Year), y = ~Civil_Liberties_Index, type = "box",
marker = list(color = "green"))
# Customize layout
fig1 <- fig1 %>% layout(title = "Civil Liberties Index in Key Historical Years",
xaxis = list(title = "Year"),
yaxis = list(title = "Civil Liberties Index"),
showlegend = FALSE)
fig1
# Line Plot: Trends by Region
# Ensure Year is treated as numeric
ggplot(df_region_bar, aes(x = Year, y = Civil_Liberties_Index, color = Region)) +
geom_line() +
facet_wrap(~ Region, scales = "free_y") +
ggtitle("Civil Liberties Index Trends by Region") +
xlab("Year") +
ylab("Civil Liberties Index") +
theme_minimal()