YuLab-SMU / ggbreak

:broken_heart: An implementation of scale functions for setting axis breaks of a 'gg' plot.
https://www.frontiersin.org/articles/10.3389/fgene.2021.774846
131 stars 4 forks source link

Removing plot border #57

Open sraul1 opened 1 year ago

sraul1 commented 1 year ago

This might just be user error, but I have not been able to figure out a way to remove the plot borders that surround the axis labels. Any suggestions? Thought it would be somewhere in the theme options but have not had any success. Thanks ahead of time!

theme_set(theme_classic(base_size = 12))
theme_update(axis.text = element_text(color = "black"),
             axis.ticks = element_line(color = "black"))
ggplot()+
  geom_point(data = stream, aes(x = date, y=no3_mgl, fill = watershed,shape = watershed), size = 1.5, color = "black")+
  scale_shape_manual(values = c(21,22,24))+
  scale_fill_manual(values = c("red", "blue", "grey"))+
  ylim(0,10)+
  scale_y_break(c(2,8), ticklabels = c(8,9), scales = c(0.3))+
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.line.y.right = element_blank(),
        axis.text.y.right = element_blank(),
        axis.ticks.y.right = element_blank(),
        panel.grid = element_blank(),
        plot.background = element_blank(),
        strip.background = element_blank())

Rplot

alaindanet commented 1 year ago

I had the problem than you and I have tried so much to remove those extra borders. I think that it is related to strange interactions between themes and ggbreak.

My workaround is to set a theme() that does not interfere with ggbreak and to call the theme() just at the plot construction.

library(tidyverse)
library(magrittr)
#> 
#> Attachement du package : 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
library(ggbreak)
#> ggbreak v0.1.1
#> 
#> If you use ggbreak in published research, please cite the following
#> paper:
#> 
#> S Xu, M Chen, T Feng, L Zhan, L Zhou, G Yu. Use ggbreak to effectively
#> utilize plotting space to deal with large datasets and outliers.
#> Frontiers in Genetics. 2021, 12:774846. doi: 10.3389/fgene.2021.774846
set.seed(2019-01-19)
d <- data.frame(
  x = 1:20,
  y = c(rnorm(5) + 4, rnorm(5) + 20, rnorm(5) + 5, rnorm(5) + 22),
  group = c(rep("A", 10), rep("B", 10)),
  face=c(rep("C", 5), rep("D", 5), rep("E", 5), rep("F", 5))
)

p <- ggplot(d, aes(x=x, y=y)) +
  geom_col(orientation="x") +
  scale_y_reverse() +
  facet_wrap(group~.,
    scales="free_y",
    strip.position="right",
    nrow=2
    ) +
  coord_flip()
pg <- p +
  scale_y_break(c(7, 17), scales="free") +
  scale_y_break(c(19, 21), scales="free")

# Good border
pg + theme_bw()


# Wrong border
theme_set(theme_bw())
pg + theme_bw()


# Good border again
theme_set(theme_grey())
pg + theme_bw()

Created on 2023-04-20 by the reprex package (v2.0.1)