linkET | 完美解决ggcor安装失败方案(附教程) Science神图

linkET | 完美解决ggcor安装失败方案(附教程)

Science神图

最近看了一个出自Science的神图,在网上搜遍教程,踩了好多坑,在这里分享一下完美解决方案~ (•‿•)

1. 方法一

这个包最难的不是使用,而是安装,通过git已经无法安装,这里提供一个通过gitee完美安装ggcor的方法

通过Gitee地址安装(原github地址已不可用)

# install.packages("devtools")
devtools::install_git("https://gitee.com/dr_yingli/ggcor")

2. 方法二

  • 安装linkET, 新版ggcor ~

  • 还是熟悉的配方,熟悉的味道~

  • …ᴅᴜᴅᴜ!

# install.packages("devtools")
devtools::install_github("Hy4m/linkET", force = TRUE)
packageVersion("linkET")

3. linkET教程

  • 由于ggcorlinkET语法几乎一样,所以在这仅介绍linkET的用法

  • 示例数据使用mtcars

3.1 加载包和数据
library(linkET)
## matrix_data
matrix_data(list(mtcars = mtcars))
#> A matrix data object:
#> Number:  1 
#> Names:  mtcars 
#> Dimensions:  32 rows, 11 columns
#> Row names:  Mazda RX4, Mazda RX4 Wag, Datsun 710, Hornet 4 Drive, Hor... 
#> Column names:  mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb

3.2 转换格式
## md_tbl
matrix_data(list(mtcars = mtcars)) %>% 
  as_md_tbl()
#> # A tibble: 352 × 3
#>    .rownames         .colnames mtcars
#>  * <chr>             <chr>      <dbl>
#>  1 Mazda RX4         mpg         21  
#>  2 Mazda RX4 Wag     mpg         21  
#>  3 Datsun 710        mpg         22.8
#>  4 Hornet 4 Drive    mpg         21.4
#>  5 Hornet Sportabout mpg         18.7
#>  6 Valiant           mpg         18.1
#>  7 Duster 360        mpg         14.3
#>  8 Merc 240D         mpg         24.4
#>  9 Merc 230          mpg         22.8
#> 10 Merc 280          mpg         19.2
#> # … with 342 more rows

## as method
as_matrix_data(mtcars)
#> A matrix data object:
#> Number:  1 
#> Names:  mtcars 
#> Dimensions:  32 rows, 11 columns
#> Row names:  Mazda RX4, Mazda RX4 Wag, Datsun 710, Hornet 4 Drive, Hor... 
#> Column names:  mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb

as_md_tbl(mtcars)
#> # A tibble: 352 × 3
#>    .rownames         .colnames mtcars
#>  * <chr>             <chr>      <dbl>
#>  1 Mazda RX4         mpg         21  
#>  2 Mazda RX4 Wag     mpg         21  
#>  3 Datsun 710        mpg         22.8
#>  4 Hornet 4 Drive    mpg         21.4
#>  5 Hornet Sportabout mpg         18.7
#>  6 Valiant           mpg         18.1
#>  7 Duster 360        mpg         14.3
#>  8 Merc 240D         mpg         24.4
#>  9 Merc 230          mpg         22.8
#> 10 Merc 280          mpg         19.2
#> # … with 342 more rows

3.3 计算相关性矩阵
## special function for correlation matrix
correlate(mtcars) %>% 
  as_matrix_data()
#> A matrix data object:
#> Number:  4 
#> Names:  r, p, lower_ci, upper_ci 
#> Dimensions:  11 rows, 11 columns
#> Row names:  mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb 
#> Column names:  mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb

correlate(mtcars) %>% 
  as_md_tbl()
#> # A tibble: 121 × 6
#>    .rownames .colnames      r        p lower_ci upper_ci
#>  * <chr>     <chr>      <dbl>    <dbl>    <dbl>    <dbl>
#>  1 mpg       mpg        1     0          1         1    
#>  2 cyl       mpg       -0.852 6.11e-10  -0.926    -0.716
#>  3 disp      mpg       -0.848 9.38e-10  -0.923    -0.708
#>  4 hp        mpg       -0.776 1.79e- 7  -0.885    -0.586
#>  5 drat      mpg        0.681 1.78e- 5   0.436     0.832
#>  6 wt        mpg       -0.868 1.29e-10  -0.934    -0.744
#>  7 qsec      mpg        0.419 1.71e- 2   0.0820    0.670
#>  8 vs        mpg        0.664 3.42e- 5   0.410     0.822
#>  9 am        mpg        0.600 2.85e- 4   0.318     0.784
#> 10 gear      mpg        0.480 5.40e- 3   0.158     0.710
#> # … with 111 more rows

3.4 Heatmap展示数据
library(ggplot2)
matrix_data(list(mtcars = mtcars)) %>% 
  hyplot(aes(fill = mtcars)) +
  geom_tile()


3.5 以dot的形式展示数据
as_md_tbl(mtcars) %>% 
  hyplot(aes(size = mtcars)) +
  geom_point(shape = 21, fill = NA)


3.6 渐变方格展示相关性~

(๑ ꒪ꌂ꒪๑)

correlate(mtcars) %>% 
  as_md_tbl() %>% 
  qcorrplot() +
  geom_square()

4. 两矩阵相关性计算

4.1 加载包和示例数据
library(vegan)
#> 载入需要的程辑包:permute
#> 载入需要的程辑包:lattice
#> This is vegan 2.5-7
data("varespec")
data("varechem")

示例数据1 - Varechem


示例数据2 - varespec


4.2 快速绘制相关性矩阵图
correlate(varespec[1:30], varechem) %>% 
  qcorrplot() +
  geom_square() +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))


4.3 只展示左下
qcorrplot(varespec[1:30], type = "lower") +
  geom_square() +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))
#> The input data is not a correlation matrix,
#> you can override this behavior by setting the `is_corr` parameter.

5. Mantel test

Mantel test 计算的是两个不相似矩阵之间的相关性。 生态学上的意义是验证环境相似的地方是否物种也相似;环境不相似的地方物种是否不相似。

也可以应用到两个基因表达矩阵之间,如mRNA表达矩阵和miRNA表达矩阵

5.1 加载包和数据
library(dplyr)
#> 
#> 载入程辑包:'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data("varechem", package = "vegan")
data("varespec", package = "vegan")
5.2 进行mantel test并新增连线数据
mantel <- mantel_test(varespec, varechem,
                      spec_select = list(Spec01 = 1:7,
                                         Spec02 = 8:18,
                                         Spec03 = 19:37,
                                         Spec04 = 38:44)) %>% 
  mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
                  labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
         pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))
#> `mantel_test()` using 'bray' dist method for 'spec'.
#> `mantel_test()` using 'euclidean' dist method for 'env'.
5.3 开始画图啦 !~ •̀ᴗ• •̀ᴗ•́
qcorrplot(correlate(varechem), type = "lower", diag = FALSE) +
  geom_square() +
  geom_couple(aes(colour = pd, size = rd), 
              data = mantel, 
              curvature = nice_curvature()) +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu")) +
  scale_size_manual(values = c(0.5, 1, 2)) +
  scale_colour_manual(values = color_pal(3)) +
  guides(size = guide_legend(title = "Mantel's r",
                             override.aes = list(colour = "grey35"), 
                             order = 2),
         colour = guide_legend(title = "Mantel's p", 
                               override.aes = list(size = 3), 
                               order = 1),
         fill = guide_colorbar(title = "Pearson's r", order = 3))

作为ggcor的接班人,linkET真是颜值担当啊!~


6. 其他的图

linkET包还提供了其他的可视化方式,大家有兴趣继续探索吧

6.1 pairs plot
qpairs(iris) + geom_pairs()

ggpairs


6.2 network
correlate(mtcars) %>% 
  as.igraph() %>% 
  plot(layout = layout_with_circular)


参考资料

  1. Sunagawa, S., Coelho, L. P., Chaffron, S., Kultima, J.R., Labadie, K., Salazar, G., … & Bork, P. (2015). Structure and function of theglobal ocean microbiome. Science, 348(6237), 1261359-1261359.
  2. Houyun Huang(2021). linkET: Everything is Linkable. R package version 0.0.3.

最后祝大家早日不卷!~

点个在看吧各位~ ✐.ɴɪᴄᴇ ᴅᴀʏ 〰

  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值