Home > OS >  R markdown: remove white space around ggmap() plot automatically without manual fig.heigh/fig.width
R markdown: remove white space around ggmap() plot automatically without manual fig.heigh/fig.width

Time:08-17

I'd like to create an html output with R markdown that includes a collection of plots made with ggmap(). When the map (or maps, in case of facets) has more width than height, there is white space above and below the plot in the html output, which I would like to remove automatically without much extra work.

White space has been discussed here previously. One solution I found is to specify fig.height and fig.width appropriately (by trying out manually). However, I would rather avoid having to try out suitable height/width values for each plot, as each of my plots comes in different height/width ratios.

A previous idea has been to figure out the width/height ratio of the plot and then to specify fig.asp: enter image description here



Originally:

I used the function get_dims() from the package DeLuciatoR. This isn't a Cran package; you have to get it through Github.

devtools::install_github("infotroph/DeLuciatoR")

If you ran your plot with this function, you'll get the best dimensions. However, if you preset the graph size and then run it, you'll get whatever your preset is (i.e., fig.height, fig.width). Why does this matter? Well, R Markdown presets the size of your graphs. You have to work around this constraint.

First, I'm going to show you the basic premise of how this works. Next, I'll show you how you can incorporate this into R Markdown.

For this first graph, I added color to the background, so you could see what I did with the margins. (What is there; what isn't there; all that jazz...)

I added borders to all the images so that you could see the difference between what is SO and the image.

library(tidyverse)
library(ggmap)
library(DeLuciatoR)
ggp <- ggmap(map_data)  
  geom_point(data = df,
             aes(x = lon, y = lat), size = 2)  
  facet_wrap(~ species, ncol = 2)   
  coord_fixed()                                         # lat/lon equidistant
  theme(plot.margin = unit(rep(.1, 4), "cm"),           # make the plot margin 1 mm
        plot.background = element_rect(fill = "green"),
        panel.background = element_rect(fill = "blue")) # show me my margins

enter image description here


Now that the graph object is created get the dimensions.

get_dims(ggp, maxwidth = 7, maxheight = 8)
# $height
# [1] 2.229751
# 
# $width
# [1] 7
#  

That's pretty awesome IMO.

coord_fixed() is part of what makes this work in R Markdown. (It forces the issue with the preset sizes.) An alternative would be to use this get_dims() call outside of your RMD (yuck). There are probably many ways to set this up so that RMD doesn't run over the best dimensions. You'll have to try it out and see what works for you.

Okay, incorporating this into RMD...

You're going to split the graphs into two separate chunks. First, call the graph to an object (ggp here) and call the dimensions (ggpd here). In a separate chunk, print the graph with the dimensions in the chunk options. In the 2nd chunk, ggpd$height is the setting for fig.height; ggpd$width is the fig.width.

My change; no issues!

```{r whatChuGot,echo=FALSE,message=FALSE,cache=TRUE}

df <- data.frame(lon = c(14.04, 14.06), lat = c(53.04, 53.07), 
                 species = c("species_1", "species_2"))
cbbox <- make_bbox(lon = c(14.0, 14.2), lat = c(53.0, 53.1), f = .1)
map_data <- get_map(location = cbbox,  source = "stamen")
ggp <- ggmap(map_data)  
  geom_point(data = df,
             aes(x = lon, y = lat), size = 2)  
  facet_wrap(~ species, ncol = 2)   
  coord_fixed()  
  theme(plot.margin = unit(rep(.1, 4), "cm"),
        plot.background = element_rect(fill = "green"),
        panel.background = element_rect(fill = "blue"))

ggpd <- get_dims(ggp, maxwidth = 7, maxheight = 8)
# $height
# [1] 2.229751
# 
# $width
# [1] 7
#      
```
The dims are `r ggpd`.

```{r showMe,results="asis",fig.height=ggpd$height, fig.width=ggpd$width}
# make me shine
ggp
```

enter image description here


Let me know if you have any questions!

This is a shot of my entire RMD (colors are over the top, but point made). If you're not feeling the HUGE margins for html_document, you could add the style max-width: unset; for .main-container. The default is 940px, regardless of monitor size.

enter image description here

  • Related