r - 如何使用 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 之间)也会很好。谢谢您的帮助!
解决方案
我使用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))
推荐阅读
- apache - 如何在 Apache 配置中隐藏 URL 中的查询参数?
- python - 在 Eve 的 pre-put 回调中从数据库中获取项目的信息
- angular - TS2345:“RolesGuard”类型的参数不可分配给“CanActivate”类型的参数
- autosar - adaptive AUTOSAR - difference between function group and application?
- package - Installing Racket Packages without installing Dr Racket
- elasticsearch - 如何使用映射将符号映射到弹性搜索中的单词?
- sql - Oracle 嵌套表约束
- outlook - 64 位应用程序中的 .msg 预览不起作用
- c# - 如果存在,如何删除最后一个字符串元素不必要的文本?
- vb.net - 需要 STMP 身份验证