【问题标题】:Density plot for multiple groups in ggplotggplot中多个组的密度图
【发布时间】:2018-11-14 23:03:31
【问题描述】:

我看过example1How to overlay density plots in R?Overlapped density plots in ggplot2 关于如何制作密度图。我可以使用第二个链接中的代码制作密度图。但是我想知道如何在ggplotplotly 中制作这样的图表? 我查看了所有示例,但无法解决我的问题。 我有一个带有基因表达leukemia data description 的玩具数据框,其中的列指的是 2 组个体

leukemia_big <- read.csv("http://web.stanford.edu/~hastie/CASI_files/DATA/leukemia_big.csv")

df <- data.frame(class= ifelse(grepl("^ALL", colnames(leukemia_big),
                 fixed = FALSE), "ALL", "AML"), row.names = colnames(leukemia_big))

plot(density(as.matrix(leukemia_big[,df$class=="ALL"])), 
     lwd=2, col="red")
lines(density(as.matrix(leukemia_big[,df$class=="AML"])), 
      lwd=2, col="darkgreen")

【问题讨论】:

    标签: r ggplot2 plotly density-plot


    【解决方案1】:

    ggplot 需要整齐的格式化数据,也称为长格式化数据框。 下面的例子将做到这一点。但请注意,提供的数据集按患者类型具有几乎相同的值分布,因此当您绘制 ALL 和 AML 类型的患者时,曲线重叠并且您看不到差异。

    library(tidyverse)
    
    leukemia_big %>% 
    as_data_frame() %>% # Optional, makes df a tibble, which makes debugging easier
    gather(key = patient, value = value, 1:72) %>% #transforms a wide df into a tidy or long df
    mutate(type = gsub('[.].*$','', patient)) %>% #creates a variable with the type of patient
    ggplot(aes(x = value, fill = type)) + geom_density(alpha = 0.5)
    

    在第二个示例中,我将为所有 AML 类型的患者的值变量添加 1 个单位,以直观地演示重叠问题

    leukemia_big %>% 
    as_data_frame() %>% # Optional, makes df a tibble, which makes debugging easier
    gather(key = patient, value = value, 1:72) %>% #transforms a wide df into a tidy or long df
    mutate(type = gsub('[.].*$','', patient)) %>% #creates a variable with the type of patient
    mutate(value2 = if_else(condition = type == "ALL", true = value, false = value + 1)) %>% # Helps demonstrate the overlapping between both type of patients
    ggplot(aes(x = value2, fill = type)) + geom_density(alpha = 0.5)`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 2012-08-16
      • 2016-05-30
      • 2015-12-26
      相关资源
      最近更新 更多