Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 - remove panel top border

Tags:

r

ggplot2

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

like image 633
DJV Avatar asked Jan 20 '20 11:01

DJV


2 Answers

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())
like image 155
Yuriy Barvinchenko Avatar answered Oct 12 '22 21:10

Yuriy Barvinchenko


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!

like image 41
JaiPizGon Avatar answered Oct 12 '22 23:10

JaiPizGon