0

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

The plot

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)
  • 3
    Not able to reproduce your issue using e.g. `theme_bw()` but in general you could set the lineheight using e.g. `theme(axis.title.y = element_text(lineheight = 1))`. – stefan Jan 02 '23 at 16:19
  • This may reduce white spacing https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression – Yacine Hajji Jan 02 '23 at 16:21
  • 1
    You can use `labs(y = stringr::str_wrap(...., width = 50)` which makes a nice break – MarBlo Jan 02 '23 at 16:21
  • 1
    I have edited my post and added a reproducible example. It appears, that the plot is printed correctly, but the axis-title changes, if I save the plot via ```ggsave()``` – RibbitRibbit Jan 02 '23 at 20:02
  • Thanks @stefan. It works that way (```theme(axis.title.y = element_text(lineheight = 1))```) – RibbitRibbit Jan 03 '23 at 07:30

0 Answers0