8

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.

I'm looking for this:

enter image description here

library(tidyverse)

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

Will results with:

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
DJV
  • 4,743
  • 3
  • 19
  • 34

3 Answers3

9

One more option (with some advice from 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())
Yuriy Barvinchenko
  • 1,465
  • 1
  • 12
  • 17
5

Using one of the references you have posted, you end up in this script (thanks to Rudolf Cardinal and Alex Holcombe). You can use the function theme_border() to plot the borders you want. 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")))

plotwithoutborders

Hope this helps!

JaiPizGon
  • 476
  • 2
  • 8
  • Thank you for your answer! it's actually a really good work around. My only problem was that I need to read additional script and the fact that it loads many variables to my environment. They should make it as a package! :) – DJV Jan 21 '20 at 08:44
  • Error in theme_border(type = c("bottom", "left")) : could not find function "theme_border" – jzadra Jun 22 '22 at 22:13
4

Another option is a massive cheat... Use theme_classic and add a segment

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)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you you for your answer, but I have other theme(s) setting that I use which clashes with your answer. – DJV Jan 21 '20 at 08:42