<- "output"
output_dir <- c("png", "eps")[1] # modify index number to change format plot_file_format
4 Demonstration of parameter variation: solar radiation and temperature
Choose file format for generated figures:
Load source file containing the R implementation of the Weather model:
source("source/weatherModel.R")
Set up six variations of parameter settings (i.e. min_value
, max_value
, is_south_hemisphere
), assuming length of year of 365 days:
<- 0
SEED <- 365
YEAR_LENGTH <- c(FALSE, TRUE)
is_southern_hemisphere_values
<- matrix(
par_values_annual_sinusoid c(0.1, 1.5, 0.31,
-0.5, 3.3, 0.73,
1.5, 2.7, 0.06,
2.1, 4.2, 0.25,
-1.6, 5, 1,
4, 4.5, 0.02),
ncol = 3, byrow = TRUE
)
<- min(par_values_annual_sinusoid[,1] - par_values_annual_sinusoid[,3])
min_min_value <- max(par_values_annual_sinusoid[,2] + par_values_annual_sinusoid[,3])
max_max_value
<- nrow(par_values_annual_sinusoid) num_runs
Create a colour palette for plotting:
<- num_runs %/% 2
num_cold_colours <- num_runs - num_cold_colours
num_warm_colours
<- function(start, end, n) {
create_color_sequence seq(start, end, length.out = n)
}
<- function(h_range, s_range, v_range, n) {
create_color_values cbind(
h = create_color_sequence(h_range[1], h_range[2], n) / 360,
s = create_color_sequence(s_range[1], s_range[2], n) / 100,
v = create_color_sequence(v_range[1], v_range[2], n) / 100
)
}
<- rbind(
color_palette_values create_color_values(c(198.6, 299.4), c(61.6, 75.3), c(95.2, 76.4), num_cold_colours),
create_color_values(c(5.15, 67.5), c(67, 77.8), c(73.7, 86.4), num_warm_colours)
)
<- apply(color_palette_values, 1, function(x) hsv(x[1], x[2], x[3])) color_palette
Plot curves:
# Helper functions
<- function() {
plot_empty plot(c(0, 1), c(0, 1), ann = FALSE, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
}
<- function(x, y, label, cex_factor = 0.6, srt = 0) {
add_text text(x = x, y = y, labels = label, font = 4,
cex = graphic_scale * (cex_factor + font_rescale + margin_text_rescale), srt = srt)
}
# Main plotting function
<- function(par_values_annual_sinusoid, is_southern_hemisphere_values, min_min_value, max_max_value) {
plot_annual_sinusoid layout(matrix(c(1, 2, 3, 12,
4, 5, 6, 12,
7, 8, 9, 12,
10, 11, 11, 12),
nrow = 4, ncol = 4, byrow = TRUE),
widths = c(1, 10, 10, 6),
heights = c(2, 10, 10, 2))
par(cex = graphic_scale * 1.2, mar = c(0, 0, 0, 0))
# Titles
plot_empty()
for (hemisphere in c("FALSE", "TRUE")) {
plot_empty()
add_text(0.55, 0.5, paste("southern_hemisphere =", hemisphere))
}
# Y-axis titles and plots
plot_empty()
add_text(0.5, 0.5, "annual sinusoidal curve", srt = 90)
par(mar = c(2, 2, 0.1, 0.1))
for (is_southern_hemisphere in is_southern_hemisphere_values) {
plot(c(1, YEAR_LENGTH), c(min_min_value, max_max_value), type = "n", xlab = "", ylab = "")
for (i in 1:nrow(par_values_annual_sinusoid)) {
<- gen_annual_sinusoid(
curve min_value = par_values_annual_sinusoid[i, 1],
max_value = par_values_annual_sinusoid[i, 2],
year_length = YEAR_LENGTH,
is_southern_hemisphere = is_southern_hemisphere)
lines(1:length(curve), curve, col = color_palette[i], lwd = graphic_scale * 3)
}
}
# Fluctuations
par(mar = c(0, 0, 0, 0))
plot_empty()
add_text(0.5, 0.5, "annual sinusoidal curve\nwith fluctuations", cex_factor = 0.5, srt = 90)
par(mar = c(2, 2, 0.1, 0.1))
for (is_southern_hemisphere in is_southern_hemisphere_values) {
plot(c(1, YEAR_LENGTH), c(min_min_value, max_max_value), type = "n", xlab = "", ylab = "")
for (i in 1:nrow(par_values_annual_sinusoid)) {
<- gen_annual_sinusoid_with_fluctuation(
curve min_value = par_values_annual_sinusoid[i, 1],
max_value = par_values_annual_sinusoid[i, 2],
year_length = YEAR_LENGTH,
is_southern_hemisphere = is_southern_hemisphere,
fluctuation = par_values_annual_sinusoid[i, 3],
seed = SEED
)
lines(1:length(curve), curve, col = color_palette[i], lwd = graphic_scale * 1)
}
}
par(mar = c(0, 0, 0, 0))
# X-axis title
plot_empty()
plot_empty()
add_text(0.5, 0.4, "day of year")
# Legend
plot(c(0, 1), c(0, nrow(par_values_annual_sinusoid) + 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
<- 0.315
x_pos <- c(0.5, -0.1, -0.4)
y_pos <- 1
jump
for (i in 1:nrow(par_values_annual_sinusoid)) {
legend(x = 0, y = (y_pos[1] + jump * i),
legend = substitute(paste("min_value = ", minValue, ","),
list(minValue = par_values_annual_sinusoid[i, 1])),
col = color_palette[i], lwd = graphic_scale * 6,
cex = graphic_scale * (0.5 + font_rescale), bty = "n")
text(x = x_pos, y = (y_pos[2] + jump * i),
labels = substitute(paste("max_value = ", max_value, ","),
list(max_value = par_values_annual_sinusoid[i, 2])),
cex = graphic_scale * (0.5 + font_rescale), adj = 0)
text(x = x_pos, y = (y_pos[3] + jump * i),
labels = substitute(paste("fluctuation = ", fluctuation),
list(fluctuation = par_values_annual_sinusoid[i, 3])),
cex = graphic_scale * (0.5 + font_rescale), adj = 0)
}
}
# Main execution
<- file.path(output_dir, paste0("Fig4-annualSinusoidCurve.", plot_file_format))
plot_name
if (plot_file_format == "png") {
<- 1
graphic_scale <- 0.5
font_rescale <- 2
axis_text_rescale <- -0.1
margin_text_rescale png(plot_name, width = graphic_scale * 800, height = graphic_scale * 400)
else if (plot_file_format == "eps") {
} <- 1.2
graphic_scale <- 1
font_rescale <- 1
axis_text_rescale <- -0.7
margin_text_rescale ::loadfonts(device = "postscript")
extrafont::cairo_ps(filename = plot_name, pointsize = 12,
grDeviceswidth = graphic_scale * 10, height = graphic_scale * 6,
onefile = FALSE, family = "sans")
}plot_annual_sinusoid(par_values_annual_sinusoid, is_southern_hemisphere_values, min_min_value, max_max_value)
dev.off()
svg
2
::include_graphics(plot_name) knitr