简体   繁体   English

ggplot2 - 删除面板顶部边框

[英]ggplot2 - remove panel top border

I'm want to remove only the top part of my graph.我只想删除图表的顶部。 I found some directions here and here .我在这里这里找到了一些方向。 However, they remove all the borders or the top and left.但是,它们会删除所有边框或顶部和左侧。 I know that I should probably use the argument panel.border with element_blank() or element_rect() but I cannot find the correct way to define it.我知道我可能应该将参数panel.borderelement_blank()element_rect()但我找不到定义它的正确方法。

I'm looking for this :我在找这个

在此处输入图片说明

library(tidyverse)

mtcars %>% 
  ggplot(aes(factor(cyl), disp)) + 
  geom_boxplot() + 
  jtools::theme_apa() + 
  theme(
    panel.border = element_blank())

Will results with:将导致:

在此处输入图片说明

Another option is a massive cheat... Use theme_classic and add a segment另一种选择是大量作弊...使用theme_classic并添加一个片段

library(tidyverse)

mtcars %>% 
  ggplot(aes(factor(cyl), disp)) + 
  geom_boxplot() + 
  #jtools::theme_apa() + 
  theme_classic() +
  annotate(geom = 'segment', x= Inf, xend = Inf, y = -Inf, yend = Inf)

Created on 2020-01-20 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 1 月 20 日创建

One more option (with some advice from Tjebo)还有一个选择(来自 Tjebo 的一些建议)

library(tidyverse)

mtcars %>% 
  ggplot(aes(factor(cyl), disp)) + 
  geom_boxplot() + 
  scale_y_continuous(sec.axis = sec_axis(~ .))+
  jtools::theme_apa() +
  theme(
    axis.line.x.bottom = element_line(color = 'black'),
    axis.line.y.left   = element_line(color = 'black'),
    axis.line.y.right  = element_line(color = 'black'),
    axis.text.y.right  = element_blank(),
    axis.ticks.y.right = element_blank(),
    panel.border       = element_blank())

Using one of the references you have posted, you end up in this script (thanks to Rudolf Cardinal and Alex Holcombe).使用您发布的参考资料之一,您最终会使用脚本(感谢 Rudolf Cardinal 和 Alex Holcombe)。 You can use the function theme_border() to plot the borders you want.您可以使用函数theme_border()来绘制您想要的边框。 To do so, just download the script provided in the link, put it in your working directory and execute the following code:为此,只需下载链接中提供的脚本,将其放入您的工作目录并执行以下代码:

library(tidyverse)
library(grid)
source("rnc_ggplot2_border_themes_2013_01.r")
mtcars %>% 
  ggplot(aes(factor(cyl), disp)) + 
  geom_boxplot() + 
  jtools::theme_apa() + 
  theme(
    panel.border = theme_border(type = c("bottom","right","left")))

情节无国界

Hope this helps!希望这可以帮助!

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

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