-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-first-R-script.R
111 lines (80 loc) · 2.77 KB
/
my-first-R-script.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
#### LOADING PACKAGES ####
library(tidyverse)
# install.packages("here")
library(here)
# here("R", "my-first-R-script.R")
# "R/my-first-R-script.R"
####### LOADING DATA ####
data_path <- here("data","urban_population.csv")
df <- read_csv(data_path,
col_types = cols(code = col_character()))
##### MANIPULATE DATA #####
df_europe <- filter(df, continent == "EUROPE")
df_europe <- select(df_europe, continent, country, year, city_size, population_in_cities)
# old pipe: %>%
df_europe <- df |>
filter(continent=="EUROPE") |>
select(continent, country, year, city_size, population_in_cities, percentage_of_population)
df_newcol <- df |>
mutate(population_in_1000 = population_in_cities / 1000) |>
mutate(data_type = case_when(
year < 2020 ~ "true data",
year >= 2020 ~ "prediction")
)
df_newcol <- df |>
mutate(population_in_1000 = population_in_cities / 1000,
data_type = case_when(
year < 2020 ~ "true data",
year >= 2020 ~ "prediction")
)
df_grouped <- df |>
group_by(city_size, continent) |>
summarise(average_perc_population_in_city = mean(percentage_of_population))
df_grouped_1950 <- df |>
filter(year == 1950) |>
group_by(city_size, continent) |>
summarise(average_perc_population_in_city = mean(percentage_of_population))
df_grouped_2035 <- df |>
filter(year == 2035) |>
group_by(city_size, continent) |>
summarise(average_perc_population_in_city = mean(percentage_of_population))
###### PRETTY PICTURES ######
plot_1950 <- df_grouped_1950 |>
ggplot(aes(x = city_size,
y = average_perc_population_in_city,
fill = continent))
plot_1950 +
geom_bar(stat="identity",
position = position_dodge())
plot_1950 +
geom_col(position = position_dodge()) +
labs(title = "Average percentage of population in cities in 1950")
plot_2035 <- df_grouped_2035 |>
ggplot(aes(x = city_size,
y = average_perc_population_in_city,
fill = continent))
plot_2035 +
geom_col(position = position_dodge()) +
labs(title = "Average percentage of population in cities in 2035")
##### Yet another pretty picture #####
library(viridis)
df |>
mutate(
city_size = fct_relevel(city_size,
c("small", "medium", "large", "very large"))
) |>
ggplot(aes(x = year,
y = percentage_of_population,
color=city_size)) +
scale_color_viridis(discrete=TRUE, direction=-1) +
geom_jitter(alpha = 0.1) +
geom_smooth(method="glm") +
facet_wrap("continent", ncol=3) +
labs(
title = "Change in city-dwelling populations on different continents",
y = "Percentage of population living in cities",
color = "Size of cities"
) +
theme_minimal()
plot_path <- here("results", "my_plot.png")
ggsave(plot_path)