0

I am using ggplot2 to generate the plot below. For some reason, it introduces titles to subplots that I want to be removed. When I remove geom_hline() lines, it somehow removes them. However, I need those horizontal lines to denote boundaries. And the subplots are generated from scale_y_cut(). Looks like there is a crosstalk between the two, that I don't understand. Your help is appreciated!

The code

library(ggsignif)
library(tidyverse)
library(ggpubr)
library(readxl)
library(rstatix)
library(stringr)
library(ggbreak)

CO2 <- CO2

ggplot(CO2, aes_string(x="Plant",y="uptake", fill="Plant"))+
  geom_boxplot(outlier.alpha = 0.25, outlier.color = "red") +
  geom_point(alpha = 0.25, size = 2 )+
  facet_wrap(~Type)+
  stat_compare_means(comparisons = list(c("Qn1","Qn2")), label.y = 105  )+
  stat_compare_means(comparisons = list(c("Qc1","Qc2")), label.y = 110)+
  stat_compare_means(comparisons = list(c("Mn1","Mn2")), label.y = 115)+
  theme(axis.text.x = element_text(angle = 20))+
  geom_hline(yintercept=100, linetype="dashed", color="red", size=1)+
  geom_hline(yintercept=5, linetype="dashed", color="red", size=1)+
  geom_hline(yintercept=130)+
  geom_hline(yintercept=0)+
  labs(y="uptake")+
  scale_y_cut(breaks=c(7, 50, 95), which=c(1,2,3,4), scales=c(1,0.25,3,0.25))

The plot

It will also be nice to remove those crowded tick marks (between 0-6 & 50-100) from the plot if possible. Thank you for your help!

1 Answers1

0

I found an answer using gg.gap library

Hope this helps others in need.

plot <- gg.gap (plot = plot,
          segments = c((break_lims[4]+break_lims[4]*0.1),(break_lims[2]*0.99)),
          ylim = c(0,(break_lims[2]*1.25)),
          tick_width = c(tick_width[[i]][1],tick_width[[i]][2]),
          rel_heights = c(0.75,0,0.25))

However, this removes the legend. But you can add the legend using add.legend(plot = plot, margin = c(top=575,right=1,bottom=1,left=1))

Dharman
  • 30,962
  • 25
  • 85
  • 135