使用Sankey图通过ggplot查看R层积和化妆品中的数据流

如何解决使用Sankey图通过ggplot查看R层积和化妆品中的数据流

我有一个治疗前(共识)和治疗后(单一药物)患者分类的数据表,我想显示患者在治疗前后如何流入不同的分类。在这种情况下,实际簇数并不重要,重要的一点是,对于大多数患者而言,在治疗前会聚在一起,在治疗后也会聚在一起。一些移动。

这是数据的屏幕截图

enter image description here

dummy dataset 

structure(list(Stimulation = c("3S","3S","3S"),Patient.ID =       c("S3077497","S1041120","S162465","S563275","S2911623","S3117192","S2859024","S2088278","S3306185","S190789","S12146451","S2170842","S115594","S2024203","S1063872","S2914138","S303984","S570813","S2176683","S820460","S1235729","S3009401","S2590229","S629309","S1208256","S2572773","S3180483","S3032079","S3217608","S5566943","S5473728","S104259","S2795346","S2848989","S2889801","S2813983","S2528246","S3151923","S2592908","S2603793","S5565867","S3127064","S675629","S834679","S3011944","S5011583","S2687896","S2998620","S651963","S2104595","S2433454","S2565220","S3307762","S294778","S995510","S2476822","S140868","S1018263","S2990223","S5524130","S1042529","S999706","S363003","S2303087","S868213","S5568359","S3174542","S521782","S3294727"),`Cluster assigned consensus` = c(2,2,5,3,1,4,7,8,6,7),`Cluster assigned single drug` = c("1","1","2","3","4","5","6","7","8","8"),count = c(1,1)),row.names = c(NA,-69L),class =     c("tbl_df","tbl","data.frame"))

我是第一次参加Sankey剧集,所以我不是专家。我添加了计数列,因此每个患者的计数为1,然后可以通过该计数添加流量厚度。

我从R教程进行了修改,可视化代码在这里

library(ggplot2)
library(ggalluvial)

ggplot(data = CLL3S,aes(axis1 = `Cluster assigned consensus`,axis2 = `Cluster assigned single drug`,y = count)) +
  scale_x_discrete(limits = c("Consensus cluster","Single-drug cluster"),expand = c(.1,.1)) +
  xlab("Clusters") +
  geom_alluvium(aes(fill = `Cluster assigned consensus`)) +
  geom_stratum() +
  geom_text(stat = "stratum",aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle("Patient flow between the Consensus clusters and Single-drug treated clusters","3S stimulated patients")

这种作品,但数字并不漂亮:

enter image description here

您会看到簇号被巨大的白色空白框包围。如何将其更改为较小的内容?以及如何将框颜色编码为不同的颜色,并确保是否更改geom_alluvium(填充),以便数据流与框(共识框)的颜色匹配?

解决方法

您可以在geom_stratum中进行控制。试试这个

library(ggplot2)
library(ggalluvial)
library(RColorBrewer)

# Define the number of colors you want
nb.cols <- 10
mycolor1 <- colorRampPalette(brewer.pal(8,"Set2"))(nb.cols)
mycolor2 <- colorRampPalette(brewer.pal(2,"Set2"))(nb.cols)

mycolors <- c("red","blue","green","orange")

ggplot(data = CLL3S,aes(y = count,axis1 = `Cluster assigned consensus`,axis2 = `Cluster assigned single drug` 
           )) +
  scale_x_discrete(limits = c("Consensus cluster","Single-drug cluster"),expand = c(.1,.1)) +
  labs(x="Clusters") +
  geom_alluvium(aes(fill = `Cluster assigned consensus`)) +
  geom_stratum(width = 1/4,fill = c(mycolor1[1:8],mycolor1[1:8]),color = "red") +
  #geom_stratum() +
  geom_text(stat = "stratum",aes(label = after_stat(stratum))) +
  #scale_fill_manual(values = mycolors) +
  theme_minimal() +
  guides(fill=guide_legend(override.aes = list(color=mycolors)))+
  ggtitle("Patient flow between the Consensus clusters and Single-drug treated clusters","3S stimulated patients")

output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-