【问题标题】:How to remove sub-plot titles and tick values with ggplot如何使用 ggplot 删除子图标题和刻度值
【发布时间】:2021-11-10 18:19:49
【问题描述】:

我正在使用 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 之间)也会很好。感谢您的帮助!

【问题讨论】:

  • 您能否通过添加一个我们可以用来运行您的代码的数据样本来使这个可重现?
  • 您指的是哪个“滴答声”?您可能想使用theme()
  • 嗨 eipi10 和 Robin Gertenbach 我已按照您的要求编辑了帖子。
  • 再次感谢您的帮助。我找到了gg.gap 库的替代方案,可以轻松绘制间隙图。感谢您的宝贵时间!

标签: r ggplot2 geom-hline


【解决方案1】:

我使用 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))添加图例

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2015-10-25
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多