-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairports_domestic.R
131 lines (116 loc) · 3.5 KB
/
airports_domestic.R
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Import required libraries
library(tidyverse)
library(scales)
library(hrbrthemes)
library(RColorBrewer)
# Read the CSV file
data <- "../aggregated/domestic/city.csv"
df <- read_csv(data)
df <- df %>%
select(Year, Month, City1, City2, PaxToCity2, PaxFromCity2)
dfCity1 <- df %>%
group_by(Year, Month, City1) %>%
summarise(PassengersFrom = sum(PaxFromCity2, na.rm = TRUE))
dfCity2 <- df %>%
group_by(Year, Month, City2) %>%
summarise(PassengersTo = sum(PaxToCity2, na.rm = TRUE))
names(dfCity1)[names(dfCity1) == "City1"] <- "City"
names(dfCity2)[names(dfCity2) == "City2"] <- "City"
df <- inner_join(dfCity1, dfCity2, by = c("Year", "Month", "City"))
df$Passengers <- df$PassengersFrom + df$PassengersTo
df <- df %>% select(-PassengersFrom, -PassengersTo)
# Keep only big cities
city_passenger_sums <- df %>%
group_by(City) %>%
summarise(total_passengers = sum(`Passengers`))
selected_cities <- city_passenger_sums %>%
filter(total_passengers >= 20000000) %>%
pull(City)
df <- df %>%
filter(City %in% selected_cities)
# Group by year and month
df <- df %>%
mutate(Date = as.Date(paste(Year, sprintf("%s", Month), "01", sep = "-"))) %>%
select(City, Date, `Passengers`) %>%
distinct()
# Order cities by their final passenger numbers
cities <- df %>%
group_by(City) %>%
filter(Date == max(Date)) %>%
arrange(desc(`Passengers`)) %>%
pull(City)
# Set factor levels in reverse order to show top cities at the top of the legend
df$City <- factor(df$City, levels = rev(cities))
# Create the plot
p <- ggplot(df, aes(
x = Date,
y = `Passengers`,
group = City,
color = City
)) +
geom_line(
data = df %>% filter(!City %in% cities),
alpha = 0.4,
size = 0.5
) +
geom_line(data = df %>% filter(City %in% cities), size = 1.2) +
ggtitle("Domestic air ridership in India (by city)") +
# Modify x-axis scale
scale_x_date(
date_breaks = "1 year",
date_labels = "%Y",
expand = c(0.02, 0)
) +
# Modify y-axis scale
scale_y_continuous(
labels = scales::comma_format(scale = 1e-6, suffix = "M"),
breaks = scales::pretty_breaks(n = 6),
expand = c(0.02, 0)
) +
# Apply a modified theme based on theme_ipsum() with white background and improved axis labels
theme_ipsum() +
theme(
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "white", color = NA),
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.title = element_text(size = 20, face = "bold"),
# Larger and bold axis titles
axis.title.x = element_text(
margin = margin(t = 20),
hjust = 0.5,
size = 16,
face = "bold"
),
# Centered x-axis title
axis.title.y = element_text(
margin = margin(r = 20),
hjust = 0.5,
size = 16,
angle = 90,
face = "bold"
),
# Centered y-axis title
axis.text = element_text(size = 12),
# Larger axis text
legend.position = "right",
legend.background = element_rect(fill = "white", color = NA),
legend.title = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(1, "cm"),
# Increase legend key size
legend.text = element_text(size = 10) # Adjust legend text size
) +
# Add labels
labs(x = "Year", y = "Monthly Passengers (Domestic)", color = "City") +
guides(colour = guide_legend(reverse = T))
# Print the plot
print(p)
# Save the plot with a white background
ggsave(
"airports_domestic.png",
plot = p,
width = 12,
height = 8,
dpi = 300,
bg = "white"
)