forked from BjnNowak/TidyTuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSC_Joyplot.R
135 lines (112 loc) · 2.71 KB
/
SC_Joyplot.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
132
133
134
135
library(tidyverse)
library(camcorder)
library(patchwork)
library(showtext)
library(ggtext)
library(glue)
library(sf)
library(terra)
library(ggridges)
library(raster)
# Set fonts
font_add_google("Open Sans","open")
font_add_google("Acme","acme")
font_add_google("Fira Sans","fira sans")
font_add_google("Fira Sans Condensed","fira")
font_add_google("Bitter","bit")
font_add_google("Staatliches","staat")
font_add_google("Raleway","ral")
showtext_auto()
# Plot size
gg_record(
dir = file.path(tempdir(),"recording"),
device = "png",
width = 10,
height = 10,
units = "cm",
dpi = 300
)
# dataset :
# https://ghsl.jrc.ec.europa.eu/download.php?ds=pop
# tutorial :
# https://dieghernan.github.io/202205_Unknown-pleasures-R/
pop <- terra::rast('Data/france_pop_l93_2.tif')
pop_it <- terra::rast('Data/italy_pop_l93.tif')
pop_all <- terra::rast('Data/allemagne_pop_l93.tif')
terra::plot(pop_all)
# Rename layer
names(pop) <- "po"
names(pop_it) <- "po"
names(pop_all) <- "po"
nrow(pop)
# Factor to reduce nrow to 100
factor <- round(nrow(pop) / 90)
pop_agg <- aggregate(pop, factor)
pop_it_agg <- aggregate(pop_it, factor)
pop_all_agg <- aggregate(pop_all, factor)
# Replace NAs
pop_agg[is.na(pop_agg)] <- 0
pop_it_agg[is.na(pop_it_agg)] <- 0
pop_all_agg[is.na(pop_all_agg)] <- 0
# Make data frame
pop_df <- as.data.frame(pop_agg, xy = TRUE, na.rm = FALSE)%>%
mutate(po=case_when(
po>0~po+1000,
TRUE~po
))
pop_it_df <- as.data.frame(pop_it_agg, xy = TRUE, na.rm = FALSE)%>%
mutate(po=case_when(
po>0~po+1000,
TRUE~po
))
pop_all_df <- as.data.frame(pop_all_agg, xy = TRUE, na.rm = FALSE)%>%
mutate(po=case_when(
po>0~po+1000,
TRUE~po
))
# Make plots
fr<-ggplot()+
geom_density_ridges(
data=pop_df,
aes(x=x,y=y,group=y,height=po),
stat="identity",
scale=30,alpha=1,
color="white",
fill="black",
size = .25,
rel_min_height=0
)+
coord_equal()+
theme_void()+
theme(plot.background = element_rect(fill = "black"))
fr
it<-ggplot()+
geom_density_ridges(
data=pop_it_df,
aes(x=x,y=y,group=y,height=po),
stat="identity",
scale=30,alpha=1,
color="white",
fill="black",
size = .25,
rel_min_height=0
)+
coord_equal()+
theme_void()+
theme(plot.background = element_rect(fill = "black"))
it
all<-ggplot()+
geom_density_ridges(
data=pop_all_df,
aes(x=x,y=y,group=y,height=po),
stat="identity",
scale=30,alpha=1,
color="white",
fill="black",
size = .25,
rel_min_height=0
)+
coord_equal()+
theme_void()+
theme(plot.background = element_rect(fill = "black"))
all