简体   繁体   中英

Why is there no right border when printing a ggplot with a ggtheme into word?

I am creating plots in R markdown to be printed to MS word. When I create a plot using the base R plot() function, there are no borders around the printed plot. Same if I use a basic ggplot. However, if I add a theme using ggthemes, it adds borders to the top, left, and bottom of the printed plot (but not the right).

I don't know why it adds these borders, or why it omits the one on the right-hand side. It appears to occur regardless of which theme I use (I prefer theme_gdocs or theme_clean due to the white backgrounds). Instead of removing all of those borders, I would prefer to actually include borders on all four sides.

Here is my code: (hopefully the images work)

---
title: "ggplot reprex"
author: "Me"
date: "11/5/2019"
output: word_document
---
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse); library(ggthemes)

data=pressure

Base plot:

plot(data)

base plot image

ggplot:

ggplot(data=data,aes(x=temperature,y=pressure))+
  geom_point()

ggplot image

ggplot with ggtheme:

ggplot(data=data,aes(x=temperature,y=pressure))+
  geom_point()+
  theme_gdocs()

ggplot with ggtheme image

I was able to fix this issue using the plot.background modification in theme() as follows:

ggplot(data=data,aes(x=temperature,y=pressure))+
  geom_point()+
  theme_gdocs()+
  theme(plot.background=element_rect(colour="black",fill=NA,size=1))

Thanks to anyone who was working on a fix!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM