THIS QUESTION WAS EDITED. PLEASE REFER TO THE EDITED PART.
SECOND EDIT: Problem solved using theme(axis.title.y = element_text(lineheight = 1))
I have created the following plot per ggplot
To implement a y-axis title with line spacing, I used
labs(y = paste0("Mean Precipitation Height across \nall days and all years"))
What I'd like to know is how to reduce the line spacing after splitting the text in the y-axis title in two.
EDIT:
I have figured out the following: When I simply print the plot,
the spacing of the y-axis title is ok. If I use ggsave()
,
it weirdly changes.
Please refer to this reproducible code:
library(tidyverse)
library(here)
{
set.seed(123)
index = rep(c(1:10),
each = 100) ; year = rep(c(1951:1960),
each = 10, times = 10); month = rep(c(1:10),
each = 1,
times = 100)
variable_of_interest = runif(1000,
min = 0,
max = 0.4); season = rep(x = c("Winter", "Winter", "Spring", "Spring", "Spring",
"Summer", "Summer", "Summer", "Autmn", "Autmn"),
times = 100)
lat = rep(c(seq(from = 48,
to = 53,
length = 10)),
each = 100); lon = rep(c(seq(from = 7,
to = 14,
length = 10)),
each = 100)
Variable_of_Interest = as.data.frame(cbind(index, year, month, variable_of_interest, season, lat, lon))
Variable_of_Interest$month_of_year = paste0(Variable_of_Interest$year,
"-",
Variable_of_Interest$month)
#Variable_of_Interest = Variable_of_Interest[order(Variable_of_Interest$year,
# Variable_of_Interest$month),]
rownames(Variable_of_Interest) = NULL
# Transform columns from character to numeric
Variable_of_Interest = mutate_at(Variable_of_Interest,
c(1,2,3,4,6,7),
as.numeric)
}
Spatial_Means =
group_by(.data = Variable_of_Interest,
lat,
lon) %>%
summarise(mean_variable_of_interest = mean(variable_of_interest))
Spatial_Means_per_Latitudinal_Degree = ggplot(Spatial_Means) +
geom_point(aes(x = lat,
y = mean_variable_of_interest),
size = 1) +
labs(title = "Latitudinal Means of Variable of Interest Across Time",
x = "Latitude (degree)",
y = paste0("Mean Variable of Interest across \nall days and all years")) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5,
size = 40),
axis.title.x = element_text(size = 20),
axis.title.y = element_text(size = 20),
axis.text.x = element_text(size = 20,
face = "bold"),
axis.text.y = element_text(size = 20,
face = "bold"))
ggsave(filename = "Spatial-Means-per-Latitudinal-Degree.JPG",
plot = Spatial_Means_per_Latitudinal_Degree,
path = here(),
width = 10,
height = 5)