分享

gg.gap:ggplot阶截断坐标轴的优秀完美解决方案

 微生信生物 2021-01-16

写在前面

ggplot的截断坐标轴一直以来做的不是很好,plot可以有一些比较复杂应用的函数可以解决截断坐标轴问题,但是ggplot出的大部分图表似乎无能为力。这里为大家介绍gg.gap包,用于ggplot截断坐标轴的绘制。

# install.packages("gg.gap")
library(gg.gap)
library(ggplot2)


data <-
data.frame(x = c("Alpha", "Bravo", "Charlie", "Delta"),
y = c(200, 20, 10, 15))
#画图
p1 = ggplot(data, aes(x = x, y = y, fill = x)) +
geom_bar(stat = 'identity', position = position_dodge(),show.legend = FALSE) +
theme_bw() +
labs(x = NULL, y = NULL)

p1 = p1 + theme_classic()
p2 = gg.gap(plot = p1,
segments = c(25, 190),
tick_width = 10,
rel_heights = c(0.25, 0, 0.1),# 设置分隔为的三个部分的宽度
ylim = c(0, 200)
)

p2

data(mtcars)
library(ggplot2)
p<-ggplot(data = mtcars, aes(x = gear, fill = gear)) +
geom_bar() +
ggtitle("Number of Cars by Gear") +
xlab("Gears")
p

#tick_width 默认不设置的时候就是一样的间隔
gg.gap(plot=p,
segments=c(5,10),
ylim=c(0,50))

#tick_width 设置两个阶段部分的不同坐标间隔
gg.gap(plot=p,
segments=c(5,10),
tick_width = c(1,10),
ylim=c(0,50))

#segments list cantains more than one number vectors
gg.gap(plot=p,
segments=list(c(2.5,4),c(5,10)),
tick_width = c(1,0.5,10),
ylim=c(0,50))

#rel_heights 设置每个间隔部分的相对高度
# list(c(2.5,4),c(5,10)) 多个间隔的指定方法
gg.gap(plot=p,
segments=list(c(2.5,4),c(5,10)),
tick_width = c(1,0.5,10),
rel_heights=c(0.2,0,0.2,0,1),
ylim=c(0,50))

#reversed y-axis
p <- ggplot(data = mtcars, aes(x = gear, fill = gear)) +
geom_bar() +
ggtitle("Number of Cars by Gear") +
xlab("Gears")+
scale_y_continuous(trans = 'reverse')
p

gg.gap(plot=p,
segments=c(10,5),
ylim=c(15,0))

# 对于分面情况下的阶段坐标轴
library(ggplot2)
p<-ggplot(mtcars,aes(mpg,hp))+geom_point()
p1<-p+facet_wrap(~cyl,scales="free")
p1

gg.gap(plot = p1,ylim = c(60,200),segments = c(100,120))

根际互作生物学研究室 简介

根际互作生物学研究室是沈其荣教授土壤微生物与有机肥团队下的一个关注于根际互作的研究小组。本小组由袁军副教授带领,主要关注:1.植物和微生物互作在抗病过程中的作用;2 环境微生物大数据整合研究;3 环境代谢组及其与微生物过程研究体系开发和应用。团队在过去三年中在 isme J, Microbiome, PCE,SBB,Horticulture Research等期刊上发表了多篇文章。欢迎关注 微生信生物 公众号对本研究小组进行了解。

团队工作及其成果 (点击查看)

了解 交流 合作

  • 团队成员邮箱 袁军:junyuan@njau.edu.cn;文涛:2018203048@njau.edu.cn

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多