Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A ggmap too small when rendered within a Rmd file?

I generate a ggmap plot that when executed from the r script looks perfect; however, the same plot in a Rmd file looks too small with half the width and disproportionate w.r.t the points being plotted.

What setting would help making the ggmap plot to take as much space as possible? My plotting code looks like this:

library(tidyverse)
library(ggmap)
library(ggplot2)

ggmap(map) + 
  scale_colour_manual(values = colorSpec) +
  geom_point(data=df, aes(x=lon, y=lat, colour=Technology, size=score), 
             position = position_jitterdodge(jitter.width=0.01, jitter.height=0.01, seed=1)) +
  theme(legend.position="bottom", plot.title = element_text(hjust = 0.5), 
        legend.text=element_text(size=6), legend.title = element_blank())

This is the correct expected output when plotted from the r script:

enter image description here

And this is the smaller and disproportionate output when plotted from the rmd and generated PDF file:

enter image description here

like image 459
SkyWalker Avatar asked Nov 23 '25 02:11

SkyWalker


1 Answers

The issue comes not from ggplot, but from the markdown settings (e.g. figure margins). You can try by specifying the plotting area when calling the ggplot block in the markdown.

# using knitr::spin()
#+ out.width = "100%", fig.align = "center", echo=FALSE

# using Rmarkdwon
```{r, out.width = "100%", fig.align = "center", echo=FALSE}
   your ggplot code
```
# or

```{r, fig.width=10, fig.height=2, fig.fullwidth=TRUE}
    your ggplot code
```

You can also try to increase the overall width of the page with options(width="a high value here")

Note that if you are using a template, all the above options will fail as you need to modify the template css file. If you cannot do that, then you may want to consider saving the ggplot as image and import it in your document.

For the people wondering about knitr::spin(), I suggest to spend some time reading about it (here or here.

like image 150
fra Avatar answered Nov 24 '25 16:11

fra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!