首页 > 解决方案 > 如何使用 ggplot 删除子图标题和刻度值

问题描述

我正在使用 ggplot2 生成下面的图。出于某种原因,它为我想要删除的子图引入了标题。当我删除geom_hline() 行时,它会以某种方式删除它们。但是,我需要那些水平线来表示边界。子图是从scale_y_cut()生成的。好像两者之间有串扰,我不明白。感谢您的帮助!

编码

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))

剧情

如果可能的话,从图中删除那些拥挤的刻度线(0-6 和 50-100 之间)也会很好。谢谢您的帮助!

标签: rggplot2geom-hline

解决方案


我使用gg.gap库找到了答案

希望这可以帮助其他有需要的人。

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))

但是,这会删除图例。但是您可以使用添加图例add.legend(plot = plot, margin = c(top=575,right=1,bottom=1,left=1))


推荐阅读