0

I have created a plot in ggplot and am using ggtext to colorize particular words in my subtitle. While that works, every time that I render the plot, there is an excessive amount of white space at the top. The code that is helping with rendering the text is below, and this is the output. Any thoughts on what I should do?

  filter(threatened == 1,
         !is.na(year_last_seen)) %>%
  count(year_last_seen, threat_type) %>%
  pivot_wider(names_from = year_last_seen, values_from = n) %>%
  mutate_if(is.integer, as.numeric) %>% 
  mutate_all(~replace(., is.na(.), 0)) %>%
  nest(c('1900-1919', '1920-1939', '1940-1959', '1960-1979', '1980-1999', '2000-2020')) %>%
  mutate('2020_sum' = map_dbl(data, sum)) %>%
  unnest() %>%
  mutate(threat_type = fct_reorder(threat_type, `2020_sum`)) %>%
  ggplot() + 
  geom_dumbbell(aes(y = threat_type, x = `Before 1900`, xend = `2020_sum`), 
                color='#E7E3D9', size = 1.5,
                colour_x = '#1E5871', colour_xend = '#8F0700', 
                size_x = 4, size_xend = 4) + 
# This is the text that is helping me colorize particular words in the subtitle.  
labs(title = "Number of Plants Threated",
       subtitle = "Change from <b style='color:#1E5871'>< 1900</b> through <b style='color:#8F0700'>2020</b>",
       x = '',
       y = '') +
  theme(plot.subtitle = element_markdown())```
wcbrown
  • 157
  • 7
  • 4
    Please provide a minimal, reproducible example. Remove all styling details that don't matter for the problem, and provide some data (possibly made up) so the code runs. – Claus Wilke Aug 20 '20 at 14:25
  • 3
    I can't reproduce this problem using the `mtcars` dataset and your `labs()` and `theme()` function calls. As Claus mentions, it would be useful to have a minimal reproducible example. – teunbrand Aug 20 '20 at 14:44
  • I've had similar issues when manually inserting linebreaks in the subtitle text using `
    `. I've found that removing these and instead adjusting the size of the textbox using `theme(plot.subtitle = element_textbox(width = unit()))` sorts things out.
    – wurli Jul 30 '21 at 13:44

0 Answers0