【问题标题】:How to plot density curves for each column in R?如何绘制R中每一列的密度曲线?
【发布时间】:2013-05-30 08:09:03
【问题描述】:

我有一个这样的数据框w:

>head(w,3)
         V1        V2         V3        V4 V5        V6         V7        V8        V9       V10 V11        V12        V13        V14
1 0.2446884 0.3173719 0.74258410 0.0000000  0 0.0000000 0.01962759 0.0000000 0.0000000 0.5995647   0 0.30201691 0.03109935 0.16897571
2 0.0000000 0.0000000 0.08592243 0.2254971  0 0.7381867 0.11936323 0.2076167 0.0000000 1.0587742   0 0.50226734 0.51295661 0.01298853
3 8.4293893 4.9985040 2.22526463 0.0000000  0 3.6600283 0.00000000 0.0000000 0.2573714 0.8069288   0 0.05074886 0.00000000 0.59403855
         V15       V16      V17       V18      V19       V20       V21      V22         V23        V24       V25       V26       V27
1 0.00000000 0.0000000 0.000000 0.1250837 0.000000 0.5468143 0.3503245 0.000000 0.183144204 0.23026538 6.9868429 1.5774150 0.0000000
2 0.01732732 0.8064441 0.000000 0.0000000 0.000000 0.0000000 0.0000000 0.000000 0.015123385 0.07580794 0.6160713 0.7452335 0.0740328
3 2.66846151 0.0000000 1.453987 0.0000000 1.875298 0.0000000 0.0000000 0.893363 0.004249061 0.00000000 1.6185897 0.0000000 0.7792773
        V28 V29     V30       V31        V32        V33       V34       V35 V36        V37        V38       V39        V40    refseq
1 0.5543028   0 0.00000 0.0000000 0.08293075 0.18261450 0.3211127 0.2765295   0 0.04230929 0.05017316 0.3340662 0.00000000 NM_000014
2 0.0000000   0 0.00000 0.0000000 0.00000000 0.03531411 0.0000000 0.4143325   0 0.14894716 0.58056304 0.3310173 0.09162460 NM_000015
3 0.8047882   0 0.88308 0.7207709 0.01574767 0.00000000 0.0000000 0.1183736   0 0.00000000 0.00000000 1.3529881 0.03720155 NM_000016

dim(w)
[1] 37126    41

我试图在一页中绘制每列(最后一列除外)的密度曲线。似乎 ggplot2 可以做到这一点。

我根据this post试了这个:

ggplot(data=w[,-41], aes_string(x=colnames)) + geom_density()

但是这样抱怨是不行的:

Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

而且我不确定如何将此数据帧的格式转换为 ggplot2 接受的格式。或者还有其他方法可以在 R 中完成这项工作吗?

【问题讨论】:

标签: r ggplot2 dataframe


【解决方案1】:

ggplot 需要长格式的数据,如下所示:

variable  value
1 V1  0.24468840
2 V1  0.00000000
3 V1  8.42938930
4 V2  0.31737190

一旦融合成一个长数据框,您就可以按变量对所有密度图进行分组。在下面的 sn-p 中,ggplot 使用w.plot 数据框进行绘图(不需要省略最终的refseq 变量)。您可以对其进行修改以使用构面、不同的颜色、填充等。

w <- as.data.frame(cbind(
  c(0.2446884, 0.0000000, 8.4293893), 
  c(0.3173719, 0.0000000, 4.9985040), 
  c(0.74258410, 0.08592243, 2.22526463)))
w$refseq <- c("NM_000014", "NM_000015", "NM_000016")

library(ggplot2)
library(reshape2)
w.plot <- melt(w) 

p <- ggplot(aes(x=value, colour=variable), data=w.plot)
p + geom_density()

【讨论】:

  • 我也有类似的问题。在我的情况下,我有 1000 列并且没有标题名称(因此在 R 中它们被称为“X1”、“X2”等)。如果我这样做melt(df),我会收到错误Using X404, X755, X974 as id variables Error in match.names(clabs, names(xi)) : names do not match previous names。我该如何解释?
  • 啊!我使用read_table2 加载数据 - 非常快速的解决方案。但它将数据转换为tibble,而不是data.frame。因此,如果我将其转换为 data.frame 它就可以了!
【解决方案2】:

使用“reshape”包中的“melt”(您也可以使用基本的 reshape 函数,但这是一个更复杂的调用)。

require (reshape)
require (ggplot2)
long = melt(w, id.vars= "refseq")

ggplot(long, aes (value)) +
    geom_density(color = variable)

# or maybe you wanted separate plots on the same page?

ggplot(long, aes (value)) +
    geom_density() +
    facet_wrap(~variable)

在 ggplot 中还有很多其他方法可以绘制此图:请参阅 例如http://docs.ggplot2.org/0.9.3.1/geom_histogram.html。

【讨论】:

  • 您在这里用作 id 的“refseq”到底是什么?
  • @kira 这是 OP 数据集中的第 41 列,唯一不是数字的列(它看起来是来自 NCBI 参考序列数据库ncbi.nlm.nih.gov/refseq 的基因登录号,但它会是一个相当不错的无论如何都猜测为 id 列)
【解决方案3】:

这是一个使用plot 函数和一个小循环的解决方案

调用你的情节

plot(density(df[,1]), type = "n")

然后运行它来添加行

n = dim(df)[2]-1
for(i in 1:n){
lines(density(c(df[,i])))
}

【讨论】:

    【解决方案4】:

    这将创建一个 8 x 5 的密度图网格,每个图上的多条线由变量 refseq 着色...

    library(tidyverse)
    
    w_density <- w[,1:40]  # columns you want densities for
    w_density$refseq <- w$refseq  # maybe you have a variable to group by
    
    w_density %>%
        pivot_longer(!refseq, names_to = "variable", values_to = "value") %>%
        ggplot(aes(x = value, colour = refseq)) +
        geom_density(show.legend = TRUE) + 
        facet_wrap(~variable, scales = "free", ncol = 5) + 
        ggtitle("Title goes here")
    

    如果网格大小不合适并且您使用的是 Rmd,那么您可以使用块大小...

    ```{r, fig.height=20, fig.width=11}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2017-07-23
      • 2019-02-08
      相关资源
      最近更新 更多