0

Is there a way to remove the ample white space around choropleth maps without saving the plot? (Because when I save the plot, I can resize the resulting picture.) @Spacedman: I aim to get a graphic in a Quarto book publishing with bookdown.org.

nc = sf::st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)

ggplot2::ggplot(nc) + 
  ggplot2::geom_sf(ggplot2::aes(fill = SID79))

example plot

Created on 2024-12-19 with reprex v2.1.1

I learned from a StackOverflow answer, that one could resize the window to remove whitespace. But the code for doing this is missing. I tried it with negative parameters in the plot.margin line but failed. I also failed with tips from another SO post.

@Allan-Cameron: I do not know what it means to resize the window. I aim to get a graphic where the plot starts immediately after the last line of text (with only one line of space between text and graphic) and where the text after the plot begins immediately (again with only one line of space to the picture).

With other ggplot2 graphics, I did not encounter this problem of ample white space at the top and bottom of a graphic. This happens only when I use the ggplot2::geom_sf() function.

5
  • You just manually drag the window to make it less tall. There is no code involved, unless you are using a popup graphics window like X11. Or do I misunderstand you? Commented Dec 19, 2024 at 23:45
  • What's your ultimate destination for this plot? A PDF? A PNG? A graphic in a Markdown, knitr, or quarto document? An image in a shiny app? Commented Dec 22, 2024 at 21:00
  • 3
    I think you need to set the figure size in the quarto chunk options so your figure has the right aspect ratio in the document. But probably someone else has the right answer, now that we know a bit more about the problem... Commented Dec 28, 2024 at 13:59
  • 1
    geom_sf is going to use an aspect ratio that's appropriate for the CRS of your geography. That's a constraint that doesn't exist for nonspatial charts. But Quarto isn't going to set the figure size for you---either you do that through defaults or specify for this image. That's why you end up with blank space to fill that gap Commented Dec 30, 2024 at 18:12
  • @camille and @Spacedman: Yes, this is the solution!! I changed the standard height of 5 inches with fig-height: 3 to 3 inches, and the space disappeared. How easy! I am ashamed that I did not know/try to change the figure size. Commented Jan 1 at 15:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.