美文网首页
ggplot2制截断坐标轴

ggplot2制截断坐标轴

作者: 粥粥zz | 来源:发表于2022-09-25 16:21 被阅读0次

https://mp.weixin.qq.com/s/weDjiYK4tOaeENI6vzJFEw

  • coord_cartesian()+拼接
    可通过函数coord_cartesian()在图中截取特定坐标轴区间的某一区域,该函数对x轴或y轴均适用,给定坐标刻度范围即可截取。
dat <- read.delim('abundance.txt', stringsAsFactors = FALSE)
 
#使用 ggplot2 绘制相对丰度的柱形图
#此处仅为演示,未进行详细的作图参数设置
library(ggplot2)
 
p <- ggplot(dat, aes(taxonomy, value.mean, fill = group)) +
geom_col(position = position_dodge(width=0.8)) +
geom_errorbar(aes(ymin = value.mean - value.sd, ymax = value.mean + value.sd), width = 0.5, position=position_dodge(.8)) +
labs(x = NULL, y = NULL) +
theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.title = element_blank())


#使用 coord_cartesian() 分割作图结果
#分别分割 y = c(0, 4)、c(4, 20)、c(60, 90) 区间
split1 <- p + coord_cartesian(ylim = c(0, 4)) + 
        theme(legend.position='none')
split2 <- p + coord_cartesian(ylim = c(4, 20)) + 
        theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), legend.position = 'none')
split3 <- p + coord_cartesian(ylim = c(60, 90)) + 
        theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), legend.position = c(0.95, 0.7))
 
#也可使用 grid 包,组合上述分割结果,获得截断坐标轴的形式
library(grid)
 
#注:通过在 viewport() 中指定坐标放置图片,实现图片对齐效果
#x 和 y 分别用于指定所放置子图在画板中的坐标,坐标取值范围为 0~1,并使用 just 给定坐标起始位置
#width 和 height 用于指定所放置子图在画板中的高度和宽度
#这部分的数值可能比较难以调整,需要多加尝试
grid.newpage()
plot_site1 <- viewport(x = 0, y = 0, width = 1, height = 0.5, just = c('left', 'bottom'))
plot_site2 <- viewport(x = 0, y = 0.5, width = 1, height = 0.25, just = c('left', 'bottom'))
plot_site3 <- viewport(x = 0, y = 0.75, width = 1, height = 0.25, just = c('left', 'bottom'))
print(split1, vp = plot_site1)
print(split2, vp = plot_site2)
print(split3, vp = plot_site3)

#也可以使用aplot包
library(aplot)
split3/split2/split1
  • 使用ggbreak包
#使用 ggbreak 截断坐标轴
#对于 x 轴使用 scale_x_break(),对于 y 轴使用 scale_y_break()
#参数中,breaks 用于截断坐标轴,scales 用于缩放分割比例
#使用scale_y_continuous定义“断点”之前的标签;
#使用ticklabels参数自定义“断点”之后的标签;
library(ggbreak)
 
#截成两段
p + scale_y_break(breaks = c(15, 65), scales = 0.5, ticklabels = c(70, 80))
#截成三段
p + scale_y_break(breaks = c(15, 50)) + scale_y_break(breaks = c(65, 70))

#此外,ggbreak 对分面图也有效
p +
facet_wrap(~group, ncol = 3) +
scale_y_break(breaks = c(15, 65), scales = 0.5, ticklabels = c(70, 80)) +
theme(legend.position = 'none')
  • 使用gg.gap包实现截断坐标轴
#使用 gg.gap 截断坐标轴
#在 gg.gap() 中指定 ggplot2 的作图对象,并添加参数截断坐标轴
#参数中,segments 用于截断坐标轴,rel_heights 缩放截图,tick_width 用于重新指定显示的刻度轴标签长度,ylim 指定刻度轴范围
library(gg.gap)
 
#截成两段
gg.gap(plot = p, segments = c(15, 65), rel_heights = c(0.7, 0, 0.2), tick_width = c(5, 10), ylim = c(0, 90))
 
#截成三段
gg.gap(plot = p, segments = list(c(15, 50), c(65, 70)), rel_heights = c(0.4, 0, 0.1, 0, 0.1), tick_width = c(5, 10, 10), ylim = c(0, 90))

相关文章

  • ggplot2 + 截断y轴

    利用patchwork可以做出坐标轴截断的样子。

  • ggplot2学习(二)

    ggplot2基础(2)——坐标轴 ggplot2基础(1)[https://www.jianshu.com/p/...

  • ggplot坐标轴调整

    待写: ggplot2中关于坐标轴的各种调整

  • 初识R语言—ggplot2作图

    from 阿雷边学边教 初识ggplot2 ggplot2要素主要包括:背景、坐标轴、图形、标题、图例、分面、文本...

  • ggplot2画出条形图(facet_wrap)

    ggplot2坐标轴最常用参数https://zhuanlan.zhihu.com/p/25173606facet...

  • 【r<-方案|转载】ggplot坐标轴截断

    ggplot本身没有坐标轴截断的功能,所以一些文献中通过软件实现的截断图用ggplot难以实现,标准的方案是使用分...

  • R语言包lemon

    具体都有什么作用暂时还不知道,可以操作ggplot2作图的坐标轴,比如下图 他把X轴的坐标轴调整成了这个形式,让我...

  • ggplot2截断纵坐标--后续

    前情提要 ggplot2不能截断纵坐标肿么办讲了几种截断方法,各有长短,我在这篇文章发出去后收到两个反馈,特来与大...

  • 打造属于自己的主题 | R

    ggplot2出的图默认文字大小不符合文章默认要求(坐标轴标签>=6pt, 坐标轴标题>=12pt, 一般还需要加...

  • ggplot2中ylim的坑

    ​在ggplot2中很容易出现一个修改坐标轴范围的问题,喜欢用ggplot2画图的需要注意一下。ylim和coor...

网友评论

      本文标题:ggplot2制截断坐标轴

      本文链接:https://www.haomeiwen.com/subject/ehcxartx.html