首页 > 解决方案 > 为什么将带有 ggtheme 的 ggplot 打印到 word 中时没有右边框?

问题描述

我在 R markdown 中创建要打印到 MS word 的图。当我使用基本 R plot() 函数创建绘图时,打印的绘图周围没有边框。如果我使用基本的ggplot也是一样。但是,如果我使用 ggthemes 添加主题,它会在打印图的顶部、左侧和底部添加边框(但不是右侧)。

我不知道它为什么会添加这些边框,或者为什么会省略右侧的边框。无论我使用哪个主题,它似乎都会发生(由于白色背景,我更喜欢 theme_gdocs 或 theme_clean)。而不是删除所有这些边界,我宁愿实际上包括所有四个边的边界。

这是我的代码:(希望图像有效)

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

data=pressure

基图:

plot(data)

基本图图像

格图:

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

ggplot 图像

带有 ggtheme 的 ggplot:

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

带有 ggtheme 图像的 ggplot

标签: rggplot2r-markdownggthemes

解决方案


我能够使用 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))

感谢任何致力于修复的人!


推荐阅读