简体   繁体   English

R-组合冲积图

[英]R - Alluvial plots for combinations

I have been wondering how one could generate an alluvial plot using a data frame in R (eg utilizing ggalluvial package) 我一直想知道如何使用R中的数据框生成冲积图(例如,使用ggalluvial包)

I have a data frame like following 我有一个如下的数据框

   GENE UNTREATED TREATED
1    G1      FOUR    FOUR
2    G2      FOUR    FOUR
3    G3      FOUR    FOUR
4    G4      FOUR   THREE
5    G5     THREE    NONE
6    G6     THREE    NONE
7    G7       TWO   THREE
8    G8       TWO     ONE
9    G9       ONE     ONE
10  G10       TWO     ONE

I would like to generate an alluvial plot with this data. 我想用这些数据生成一个冲积图。 For example, 2 bars in the plot will be UNTREATED and TREATED and connections will be based on the proportions of how genes changing their values before and after treatment. 例如,图中的2条柱将被未处理和已处理,并且连接将基于基因在治疗前后如何改变其值的比例。 It should definitely possible to do this and I didn't seem to get the grasp of alluvial plot concept. 绝对有可能做到这一点,而我似乎并没有掌握冲积积积概念。 Any help would be highly appreciated. 任何帮助将不胜感激。

EDIT: Here is a sample sketch I made how the plot would look like 编辑:这是我制作的样例草图,情节看起来像

在此处输入图片说明

Thank you. 谢谢。

This seems to be what you are after 这似乎是你所追求的

dd<-read.table(text="   GENE UNTREATED TREATED
1    G1      FOUR    FOUR
2    G2      FOUR    FOUR
3    G3      FOUR    FOUR
4    G4      FOUR   THREE
5    G5     THREE    NONE
6    G6     THREE    NONE
7    G7       TWO   THREE
8    G8       TWO     ONE
9    G9       ONE     ONE
10  G10       TWO     ONE", header=TRUE)

ggplot(dd, aes(axis1=UNTREATED, axis2=TREATED)) +
  geom_alluvium() + 
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", label.strata = TRUE)

在此处输入图片说明

Alternatively you can use the ggalluvial wrappers that come with easyalluvial 另外,您可以使用ggalluvial附带的包装easyalluvial

 dd<-read.table(text="   GENE UNTREATED TREATED
               1    G1      FOUR    FOUR
               2    G2      FOUR    FOUR
               3    G3      FOUR    FOUR
               4    G4      FOUR   THREE
               5    G5     THREE    NONE
               6    G6     THREE    NONE
               7    G7       TWO   THREE
               8    G8       TWO     ONE
               9    G9       ONE     ONE
               10  G10       TWO     ONE", header=TRUE)


easyalluvial::alluvial_wide( dd, id = GENE, fill_by = 'all_flows',
                             order_levels = c('NONE','FOUR', 'THREE', 'TWO', 'ONE'))

易积积

MrFlick answer is very nice. MrFlick的回答非常好。 Another option would be using alluvial plots, which has been discussed in " https://github.com/topepo/caret/issues/755 ". 另一种选择是使用冲积图,已在“ https://github.com/topepo/caret/issues/755 ”中进行了讨论。

library(alluvial)
plotCM <- function(cm){
  cmdf <- as.data.frame(cm[["table"]])
  cmdf[["color"]] <- ifelse(cmdf[[1]] == cmdf[[2]], "green", "red")  
  alluvial::alluvial(cmdf[,1:2]
                 , freq = cmdf$Freq
                 , col = cmdf[["color"]]
                 , alpha = 0.5
                 , hide  = cmdf$Freq == 0
 )
}

dd<-read.table(text="   GENE UNTREATED TREATED
1    G1      FOUR    FOUR
2    G2      FOUR    FOUR
3    G3      FOUR    FOUR
4    G4      FOUR   THREE
5    G5     THREE    NONE
6    G6     THREE    NONE
7    G7       TWO   THREE
8    G8       TWO     ONE
9    G9       ONE     ONE
10  G10       TWO     ONE", header=TRUE)

labels=unique(dd$UNTREATED)
d1 <- factor(dd$UNTREATED,labels=labels)
d2 <- factor(dd$TREATED,labels=labels)
confusionMatrix(d1,d2) %>% plotCM()

在此处输入图片说明

Hope it helps. 希望能帮助到你。

Thanks, Sam 谢谢山姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM