-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimalist_topography.R
More file actions
97 lines (74 loc) · 3.98 KB
/
Copy pathminimalist_topography.R
File metadata and controls
97 lines (74 loc) · 3.98 KB
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
# Install forked version of ggridges which has been modified to include a white outline around lines
library("devtools")
install_github("robbibt/ggridges")
# Import libraries
library("raster")
library("tidyverse")
library("zoo")
library("ggridges")
library("scales")
##########
# Set up #
##########
# Input DEM raster file path
input_raster = "raw_data/AUS.tif"
output_file = "output_data/AUS.png"
# Plotting parameters, USA
plot_spacing = 18 # select every xth y-axis row for plotting (small values = finely spaced lines)
plot_exp = 0.65 # exponent factor for elevations (small values = less difference between low and high)
plot_max = 8 # maximum height in rows to scale the highest line (high values = more crossing lines)
plot_smoothing = 14 # How many x-axis points to smooth using rolling averages
# Plotting parameters, Europe
plot_spacing = 16 # select every xth y-axis row for plotting (small values = finely spaced lines)
plot_exp = 0.65 # exponent factor for elevations (small values = less difference between low and high)
plot_max = 5 # maximum height in rows to scale the highest line (high values = more crossing lines)
plot_smoothing = 14 # How many x-axis points to smooth using rolling averages
# # Plotting parameters, Mexico
# plot_spacing = 4 # select every xth y-axis row for plotting (small values = finely spaced lines)
# plot_exp = 0.8 # exponent factor for elevations (small values = less difference between low and high)
# plot_max = 5 # maximum height in rows to scale the highest line (high values = more crossing lines)
# plot_smoothing = 4 # How many x-axis points to smooth using rolling averages
# Plotting parameters, Australia
plot_spacing = 18 # select every xth y-axis row for plotting (small values = finely spaced lines)
plot_exp = 0.83 # exponent factor for elevations (small values = less difference between low and high)
plot_max = 7 # maximum height in rows to scale the highest line (high values = more crossing lines)
plot_smoothing = 13 # How many x-axis points to smooth using rolling averages
# # Plotting parameters, Canada
# plot_spacing = 26 # select every xth y-axis row for plotting (small values = finely spaced lines)
# plot_exp = 0.7 # exponent factor for elevations (small values = less difference between low and high)
# plot_max = 5 # maximum height in rows to scale the highest line (high values = more crossing lines)
# plot_smoothing = 18 # How many x-axis points to smooth using rolling averages
############
# Analysis #
############
# Import raster and set areas of low elevation to NA
dem = raster(input_raster)
plot(dem)
# Convert to dataframe of points
dem_points = as_data_frame(rasterToPoints(dem))
colnames(dem_points) = c("x", "y", "elevation")
# Convert elevation data to relative heights
dem_df = dem_points %>%
# Extract every nth unique Y value (i.e. row)
filter(y %in% unique(y)[ c( rep(FALSE, plot_spacing), TRUE ) ]) %>%
# For improved visual impact in low elevation areas, use exponent
mutate(height = ifelse(elevation > 0, elevation ^ plot_exp, NA)) %>%
# For each row, apply smoothing window to reduce noise
group_by(y) %>%
mutate(height = rollapply(height, width = plot_smoothing, FUN = mean, partial = TRUE, align = "center")) %>%
# Finally, stretch between 0 and maximum height in rows, then remove 0 elevation lines
ungroup() %>%
mutate(height = rescale(height, to = c(0, plot_max * plot_spacing * yres(dem))))
############
# Plotting #
############
dem_plot = ggplot(data = dem_df) +
geom_ridgeline(aes(x = x, y = y, height = height, group = y), fill = "white", size = 0.45) +
coord_fixed() +
theme(line = element_blank(),
text = element_blank(),
title = element_blank(),
plot.background = element_blank(),
panel.background = element_blank())
# ggsave(output_file, dem_plot, dpi = 508*2, height = 100/2, width = 70.478/2, units = "cm", type = "cairo-png")
ggsave(output_file, dem_plot, dpi = 508*2, width = 100/2, height = 70.478/2, units = "cm", type = "cairo-png")