查看原文
其他

「盘点」R语言的那些拼图方法

豆豆花花 生信星球 2022-06-25


 今天是生信星球陪你的第450天


   大神一句话,菜鸟跑半年。我不是大神,但我可以缩短你走弯路的半年~

   就像歌儿唱的那样,如果你不知道该往哪儿走,就留在这学点生信好不好~

   这里有豆豆和花花的学习历程,从新手到进阶,生信路上有你有我!

花花写于 2019-09-18

接下来的被约稿的推文预告一下:

1、R 要不要更新,以及要避开的坑

2、Rstudio如何挂载2个版本的R

3、盘点处理R语言的那些缺失值处理方法

你有什么想约的稿吗?后台告诉我,免费的~谁让我最近缺素材呢,范围限于R语言,别的我可不会。

「盘点」系列目录:

「盘点」R语言的那些分分合合

1.par里的mfrow

可用于基础包拼图

par(mfrow = c(2,2))
plot(1:100 + rnorm(100))
plot(rnorm(100), type = "l")
hist(rnorm(500))
acf(rnorm(100))

2.grid.arrange

ggplot2拼图御用

https://www.jianshu.com/p/0970fdac9071

if(!require(dplyr))install.packages("dplyr")
library(dplyr)
test <- iris %>% 
  head(120) %>% 
  tail(40) %>% 
  rbind(head(iris,20)) %>% 
  mutate(n=1:60)
table(test$Species)
#> 
#>     setosa versicolor  virginica 
#>         20         20         20
if(!require(ggplot2))install.packages("ggplot2")
library(ggplot2)
p <- ggplot(data = test) +theme_bw()
colnames(test)
#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
#> [5] "Species"      "n"

##密度图
p_density <- p + geom_density(
  aes(Sepal.Length, color = Species)
)+theme(legend.position = "none"

##箱线图
p_boxplot <- p + geom_boxplot(
  aes(x = Species,y = Sepal.Length,color = Species)
)+theme(legend.position = "none"

##小提琴图
p_violin <- p + geom_violin(
  aes(x = Species, y = Sepal.Length, fill = Species)
)+theme(legend.position = "none"

## 点图
p_point <- p + geom_point(aes(x=n,y = Sepal.Length))+theme(legend.position = "none"

## 管他什么图
p_col <- p + geom_col(aes(x=n,y=Sepal.Length,fill = Species))+theme(legend.position = "none"

## 凑数用

p_s <- p + geom_area(aes(x=n,y=Sepal.Length,fill = Species))+theme(legend.position = "none"
if(!require(gridExtra))install.packages("gridExtra")
library(gridExtra)
plots = list(p_density,p_boxplot,p_violin)
lay1 = rbind(c(12),
            c(33)) #布局矩阵
grid.arrange(grobs = plots, 
             layout_matrix = lay1,
             widths = c(12),  #第一列的宽度为1,第二列的宽度为2
             heigths = c(10.1)) #依然不知道为什么高度调整失败

让布局再复杂一点

lay2 = rbind(c(13),
            c(23)) #布局矩阵
plots2 <- list(p_s,p_col,p_point)
p1 <- arrangeGrob(grobs = plots, 
             layout_matrix = lay1)
p2 <- arrangeGrob(grobs = plots2,
             layout_matrix = lay2)
grid.arrange(p1, p2, ncol = 2)
grid.arrange(p1, p2, nrow = 2)

3.cowplot

可以给图加上ABCD编号蛮好的。

if(!require(cowplot))install.packages("cowplot")
library(cowplot)
gg <- ggdraw() +     
    draw_plot(p_boxplot, x=0, y=0.5, width=1, height=0.5) +  
    draw_plot(p_violin, 000.50.5) +   
    draw_plot(p_density, 0.500.50.5) + 
    draw_plot_label(c("A""B""C"), c(000.5), c(10.50.5), size = 15, colour = "black"
gg
未完!有续集,在下面。

向大家隆重推荐隔壁生信技能树的一系列干货!

全球公益巡讲招学徒

B站公益74小时生信工程师教学视频合辑


点击底部的“阅读原文”,获得更好的阅读体验哦😻

初学生信,很荣幸带你迈出第一步。

我们是生信星球,一个不拽术语、通俗易懂的生信知识平台。由于是2018年新号,竟然没有留言功能。需要帮助或提出意见请后台留言、联系微信或发送邮件到jieandze1314@gmail.com,每一条都会看到的哦~


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存