R繪圖往期回顧: R繪圖:gggibbous,基于ggplot2的Moon charts R繪圖:ggeconodist,基于ggplot2的另類箱圖 R繪圖:相關(guān)性分析與作圖(單基因相關(guān)性) R繪圖 雷達(dá)圖-單基因泛癌差異表達(dá)的另類展現(xiàn)形式 有的時候,只畫一半的圖,或者你一半我一半拼湊起來,會有意外的效果,而R包gghalves就可以在ggplot2的基礎(chǔ)上,畫一半的圖.
安裝加載包 if(length(getOption("CRAN"))==0) options(CRAN="https://mirrors.tuna./CRAN/") if(!require("gghalves")) BiocManager::install("gghalves") GeomHalfPoint1 在x軸上,它們占據(jù)的空間最多是分配給特定因素的空間的一半 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_point() ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_point() 其工作方式是將transformation=PositionJitter傳遞給geom。我們可以通過傳遞transformation參數(shù)來使用此轉(zhuǎn)換的默認(rèn)值 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_point(transformation_params = list(height = 0, width = 0.001, seed = 1)) 或者改變轉(zhuǎn)換參數(shù)本身 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_point(transformation = PositionIdentity) GeomHalfBoxplotGeomHalfBoxplot顯示一個被切成兩半并在x軸上分配給特定因子空間的左側(cè)或右側(cè)繪制的boxplot。 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_boxplot() 除了標(biāo)準(zhǔn)的side參數(shù)外,還可以將半盒繪圖居中,并決定是否繪制errorbar。 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_boxplot(side = "r", center = TRUE, errorbar.draw = FALSE) GeomHalfViolin半小提琴,除了side參數(shù)外,它還支持可以傳遞給標(biāo)準(zhǔn)geomviolin的所有參數(shù)。 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_violin() GeomHalfDotplotGeomHalfDotplot與其他geoms略有不同,因?yàn)樗恢С诌厖?shù),因?yàn)樗呀?jīng)通過stackdir內(nèi)置到標(biāo)準(zhǔn)GeomDotplot中 ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_half_violin() + geom_dotplot(binaxis = "y", method="histodot", stackdir="up",binwidth=0.06) 那么,既然geom_dotplot可以用作半geom,為什么需要geom_half_dotplot?原因是當(dāng)存在多個因素時,geom_dotplot不支持回避。讓我們考慮以下示例: df <- data.frame(score = rgamma(150, 4, 1), gender = sample(c("M", "F"), 150, replace = TRUE), genotype = factor(sample(1:3, 150, replace = TRUE))) 有了這些數(shù)據(jù),我們想按基因型分組,但也要按性別劃分圖。這在使用標(biāo)準(zhǔn)geom時不太管用: ggplot(df, aes(x = genotype, y = score, fill = gender)) + geom_half_violin() + geom_dotplot(binaxis = "y", method="histodot", stackdir="up", position = PositionDodge) 點(diǎn)圖與小提琴圖實(shí)際上是重疊了 使用geom_half_dotplot ggplot(df, aes(x = genotype, y = score, fill = gender)) + geom_half_violin() + geom_half_dotplot(method="histodot", stackdir="up",binwidth=0.3) Combining Different Geomslibrary(tidyverse) ggplot() + geom_half_boxplot( data = iris %>% filter(Species=="setosa"), aes(x = Species, y = Sepal.Length, fill = Species), outlier.color = NA) + ggbeeswarm::geom_beeswarm( data = iris %>% filter(Species=="setosa"), aes(x = Species, y = Sepal.Length, fill = Species, color = Species), beeswarmArgs=list(side=+1) ) + geom_half_violin( data = iris %>% filter(Species=="versicolor"), aes(x = Species, y = Sepal.Length, fill = Species), side="r") + geom_half_dotplot( data = iris %>% filter(Species=="versicolor"), aes(x = Species, y = Sepal.Length, fill = Species), method="histodot", stackdir="down") + geom_half_boxplot( data = iris %>% filter(Species=="virginica"), aes(x = Species, y = Sepal.Length, fill = Species), side = "r", errorbar.draw = TRUE, outlier.color = NA) + geom_half_point( data = iris %>% filter(Species=="virginica"), aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side = "l") + scale_fill_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) + scale_color_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) + theme(legend.position = "none") 公眾號“生信小課堂” TCGA數(shù)據(jù)分析課程TCGA數(shù)據(jù)分析大全
|
|