简体   繁体   English

为什么将带有 ggtheme 的 ggplot 打印到 word 中时没有右边框?

[英]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.我正在 R markdown 中创建要打印到 MS 字的图。 When I create a plot using the base R plot() function, there are no borders around the printed plot. 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.如果我使用基本的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).但是,如果我使用 ggthemes 添加主题,它会在打印的 plot(但不是右侧)的顶部、左侧和底部添加边框。

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).无论我使用哪个主题,它似乎都会发生(由于白色背景,我更喜欢 theme_gdocs 或 theme_clean)。 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:

plot(data)

base plot image基本 plot 图像

ggplot:格图:

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

ggplot image ggplot 图像

ggplot with ggtheme:带有 ggtheme 的 ggplot:

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

ggplot with ggtheme image带有 ggtheme 图像的 ggplot

I was able to fix this issue using the plot.background modification in theme() as follows:我能够使用 theme() 中的 plot.background 修改来解决此问题,如下所示:

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!感谢任何致力于修复的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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