gpt4 book ai didi

r - ggplot 中多个组的密度图

转载 作者:行者123 更新时间:2023-12-01 11:16:16 25 4
gpt4 key购买 nike

我看过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")

最佳答案

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)

results with original data

在第二个示例中,我将为所有 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)`

results with modified data for AML type patients

关于r - ggplot 中多个组的密度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50700432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com