本文介绍了如何在冲积/桑基图的流项上添加价值标签(在R ggalluvial上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我要在R上标记冲积/ Sankey图表的流量部分。I'm looking to label the "flow" portion of Alluvial / Sankey chart on R.层(列)可以轻松标记,但不能连接它们的流程。我阅读文档和进行实验的所有尝试均无济于事。The stratums (columns) can easily be labelled, but not the flows connecting them. All my attempts on reading the documentations and experimenting were to no avail.在下面的示例中,流连接部分应标有 freq。In the sample below, "freq" is expected to be labelled on the flow connection part.library(ggplot2)library(ggalluvial)data(vaccinations)levels(vaccinations$response) <- rev(levels(vaccinations$response))ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, y = freq, fill = response, label = freq)) + scale_x_discrete(expand = c(.1, .1)) + geom_flow() + geom_stratum(alpha = .5) + geom_text(stat = "stratum", size = 3) + theme(legend.position = "bottom") + ggtitle("vaccination survey responses at three points in time")推荐答案可以选择采用原始数字并将其用作流程部分的标签:There is an option to take the raw numbers and use these as labels for the flow part:ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, y = freq, fill = response, label = freq)) + scale_x_discrete(expand = c(.1, .1)) + geom_flow() + geom_stratum(alpha = .5) + geom_text(stat = "stratum", size = 3) + geom_text(stat = "flow", nudge_x = 0.2) + theme(legend.position = "bottom") + ggtitle("vaccination survey responses at three points in time")如果您想更好地控制如何标记这些点,则可以提取图层数据并进行计算。例如,我们可以只计算起始位置的分数,如下所示:If you want more control over how to label these points, you can extract the layer data and do computations on that. For example we can compute the fractions for only the starting positions as follows:# Assume 'g' is the previous plot object saved under a variablenewdat <- layer_data(g)newdat <- newdat[newdat$side == "start", ]split <- split(newdat, interaction(newdat$stratum, newdat$x))split <- lapply(split, function(dat) { dat$label <- dat$label / sum(dat$label) dat})newdat <- do.call(rbind, split)ggplot(vaccinations, aes(x = survey, stratum = response, alluvium = subject, y = freq, fill = response, label = freq)) + scale_x_discrete(expand = c(.1, .1)) + geom_flow() + geom_stratum(alpha = .5) + geom_text(stat = "stratum", size = 3) + geom_text(data = newdat, aes(x = xmin + 0.4, y = y, label = format(label, digits = 1)), inherit.aes = FALSE) + theme(legend.position = "bottom") + ggtitle("vaccination survey responses at three points in time")对于您到底想在哪里放置标签仍然是一种判断。从一开始就做起来很简单,但是如果您希望这些标签大约在中间并躲避另一个标签,则需要进行一些处理。It still is kind of a judgement call about where exactly you want to place the labels. Doing it at the start is the easy way, but if you want these labels to be approximately in the middle and dodging oneanother it would require some processing. 这篇关于如何在冲积/桑基图的流项上添加价值标签(在R ggalluvial上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 03:15