本文介绍了如何减少ggplot2中分类y轴之间的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下代码: library(tidyverse) dat FCCTXXX4XXX2FCCTXXX4XXX2FCCTXXX4 ,XXX2,FCCT,XXX4,XXX2,FCCT,XXX4,XXX2),tissue = c(DA,DA DA,MB,MB,MB,VL,VL,VL,UP,UP,UP,BU,BU ,BU,TV,TV,TV),motif_enrichment = c(4740, 964.2,539.2,6634,1860,1150,6312,2146,1432,5336,2282,$表达式_tpm = c(5.095, 15.1825,1.4225,7.27,23.7125,6.85,4.8775,27.17,3.0025, 6.0025,b $ b 1381,2796,1947,1175,8190,1576,926.8) (c)(3L,4L,2L,3L,6L,3L,2L,6L, 2L,3L,3L,4L,2L,3L,6L,3L,2L,6L,23.3725,5.1445,5.4525,20.215,4.695,6.44,22.04, 4.24),expr_brks = ,6L,3L,3L,6L,2L,3L,6L,2L)。标签= c(( - Inf,1],(1,5],(5,10) ,(10,16],(16,20),(20,Inf)),class =factor), motif_brks = structure(c(6L,3L,3L, 6L,5L,4 L,6L,6L, 4L,6L,6L,4L,6L,5L,4L,6L,5L,3L)。标签= c(( - Inf,100), (100,500),(500,1e + 03),(1e + 03,1.5e + 03],(1.5e + 03,2e + 03],(2e + 03 ,Inf)),class =factor)),.Names = c(motif_and_gene_name,tissue,motif_enrichment,expression_tpm,expr_brks,motif_brks ),row.names = c(NA,-18L),class = c(tbl_df,tbl,data.frame)) mycol< - c (#3D1E1F,#FE677E,#F19068,#E8A99D,#FCDED4) dat%>% ggplot(aes(x = tissue,y = motif_and_gene_name ,size = motif_brks,color = expr_brks))+ geom_point(stat =identity)+ scale_color_manual(values = colorRampPalette(mycol)(length(levels(dat $ expr_brks))))+ blab()+ ylab()+ theme_bw() 产生以下图表: 如上图所示,我如何减少间隔间隔并将x轴向上推。 解决方案 当我找到OP的作用时,我会清理这个答案。对不起,目前有点无组织。 您可以通过增加展开来减少y轴上的距离。值,在这里我已将它设置为 1.9 。 ggplot( ())+ scale_y_discrete(expand = c(0,1.9))+ theme_bw(),这是一个非常简单的方法, + bbs(x = NULL,y = NULL)+ scale_colour_manual(values = c(#3D1E1F,#FE677E,#F19068,#E8A99D,#FCDED4)) $ p $ b pre $ g $ p $ ggplot(dat,aes(tissue,motif_and_gene_name,size = motif_brks,color = expr_brks))$ b + geom_point(stat =identity)+ scale_y_discrete(expand = c(200,2))+ theme_bw()+ theme(axis.title = element_blank(),axis.text.y =元件_blank(),axis.ticks.y = element_blank())+ scale_colour_manual(values = c(#3D1E1F,#FE677E,#F19068,#E8A99D,#FCDED4) )) 可能展开不是正确的路线,而只是一个较低的比率,其中 coord_fixed()。像这样。 ggplot(dat,aes(tissue,motif_and_gene_name,size = motif_brks,color = expr_brks))+ geom_point( ()= + b_b()+ labs(x = NULL,y = NULL)+ scale_colour_brewer(palette =Set3)+ coord_fixed(ratio = .5) I have the following code:library(tidyverse)dat <- structure(list(motif_and_gene_name = c("FCCT", "XXX4", "XXX2","FCCT", "XXX4", "XXX2", "FCCT", "XXX4", "XXX2", "FCCT", "XXX4","XXX2", "FCCT", "XXX4", "XXX2", "FCCT", "XXX4", "XXX2"), tissue = c("DA","DA", "DA", "MB", "MB", "MB", "VL", "VL", "VL", "UP", "UP", "UP","BU", "BU", "BU", "TV", "TV", "TV"), motif_enrichment = c(4740,964.2, 539.2, 6634, 1860, 1150, 6312, 2146, 1432, 5336, 2282,1381, 2796, 1947, 1175, 8190, 1576, 926.8), expression_tpm = c(5.095,15.1825, 1.4225, 7.27, 23.7125, 6.85, 4.8775, 27.17, 3.0025,6.0025, 23.3725, 5.1425, 5.4525, 20.215, 4.695, 6.44, 22.04,4.24), expr_brks = structure(c(3L, 4L, 2L, 3L, 6L, 3L, 2L, 6L,2L, 3L, 6L, 3L, 3L, 6L, 2L, 3L, 6L, 2L), .Label = c("(-Inf,1]","(1,5]", "(5,10]", "(10,16]", "(16,20]", "(20, Inf]"), class = "factor"), motif_brks = structure(c(6L, 3L, 3L, 6L, 5L, 4L, 6L, 6L, 4L, 6L, 6L, 4L, 6L, 5L, 4L, 6L, 5L, 3L), .Label = c("(-Inf,100]", "(100,500]", "(500,1e+03]", "(1e+03,1.5e+03]", "(1.5e+03,2e+03]", "(2e+03, Inf]"), class = "factor")), .Names = c("motif_and_gene_name","tissue", "motif_enrichment", "expression_tpm", "expr_brks","motif_brks"), row.names = c(NA, -18L), class = c("tbl_df", "tbl","data.frame"))mycol <- c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4")dat %>% ggplot(aes(x = tissue,y = motif_and_gene_name, size = motif_brks, color = expr_brks)) + geom_point(stat = "identity") + scale_color_manual(values = colorRampPalette(mycol)(length(levels(dat$expr_brks)))) + xlab("") + ylab("") + theme_bw()Which produces the following plot:As stated in the image above, how can I reduce the inter-ticks gap and push x-axis up. 解决方案 I'll clean up this answer when I find out what works for OP. Sorry that it's a bit unorganized at the moment.You can decrease the distance on the y-axis by increasing the expand value, here I've set it to 1.9.ggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + geom_point(stat = "identity") + scale_y_discrete(expand = c(0, 1.9)) + theme_bw() + labs(x=NULL, y=NULL) + scale_colour_manual(values = c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4"))Like thisggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + geom_point(stat = "identity") + scale_y_discrete(expand = c(200, 2)) + theme_bw() + theme(axis.title =element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) + scale_colour_manual(values = c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4"))")Maybe expand is not the right rout, but simply a lower ratio with coord_fixed(). Like this-ish. ggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + geom_point(stat = "identity") + theme_bw() + labs(x=NULL, y=NULL) + scale_colour_brewer(palette = "Set3") + coord_fixed(ratio=.5) 这篇关于如何减少ggplot2中分类y轴之间的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-05 21:37