ggalluvial: 如何按照数据框中的顺序对块之间的线条进行排序?

huangapple go评论89阅读模式
英文:

ggalluvial: How to order the lines between blocks to be in same order as your data.frame?

问题

我已使用R中的ggalluvial包制作了一个淤积图。我正在尝试对流程(块之间的线条)进行排序,以便更容易阅读 - 我的目标是让它们在每个块的每个部分都堆叠在一起(橙色,然后紫色)。在这里,我将块定义为垂直条形图,在x轴上的位置1、2和3。

可重现的示例

# 创建数据框
w <- c("Rain Forest", "Desert", "Arctic", "City", "Low Earth", "Boonies", "Space")
x <- 1:40
y <- c("Bird", "Tiger", "Cat", "Parrot", "T-Rex", "Dreadwing")

set.seed(0)
GenComp <- data.frame(
  Location = sample(w, size = 28, replace = TRUE),
  Frequency = sample(x = x, size = 28, replace = TRUE),
  Animal = sample(x = y, size = 28, replace = TRUE),
  Gender = rep(c("Male", "Female"), 14))

GenComp <- GenComp[order(GenComp$Gender, GenComp$Animal, GenComp$Location, decreasing = FALSE),]
row.names(GenComp) <- 1:nrow(GenComp)

GenComp$Location <- factor(GenComp$Location, levels = sort(unique(GenComp$Location)))
GenComp$Gender <- factor(GenComp$Gender, levels = c("Female", "Male"))
GenComp$Animal <- factor(GenComp$Animal, levels = sort(unique(GenComp$Animal)))
GenComp <- GenComp[, c(4, 3, 1, 2)]

# 绘图
library(ggalluvial)
ggplot(GenComp, aes(y = Frequency, axis1 = Gender, axis2 = Animal, axis3 = Location)) + 
  geom_alluvium(aes(fill = Gender), width = 1/8, knot.pos = 0, reverse = FALSE) +
  geom_stratum(alpha = 0.25, width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), reverse = TRUE, size = 3)  +
  ggtitle("Distribution of Fauna") +
  scale_fill_manual(values = c(Female = "#691869", Male = "#EBBC55")) +
  guides(fill = "none")

这是图形:
ggalluvial: 如何按照数据框中的顺序对块之间的线条进行排序?

红色箭头指向了文本被放置在不应该的位置。蓝色圆圈是我不想要的示例 - 圆圈显示了黄色和紫色线的混合。我希望始终将黄色线放在紫色线的上方,每个块的每个部分都是这样。

尝试理解事情时,我在这篇帖子中了解到每个块内流的排序的答案是'alluvial()中流的顺序由行顺序控制。这就是为什么我已经按照我希望看到alluvia图的顺序对GenComp数据框进行了排序。然而,在ggplot2版本中,即使我重新排序行,我得到相同的图。一个解决方法是使用lode_favorlode_order命令。

如果这是答案,我如何在上面的代码中使用ggalluvia来实现这些命令?

我还在这里了解到:ggalluvial - 让层次结构的顺序遵循数据集,“你可以通过将字符向量转换为因子并指定所需的级别来重新排序轴。”这就是为什么我在上面的代码中指定了factor(..., levels = ())。但这也没有起作用。

我在这里阅读到:矩形的顺序下的部分:“手动矿矿定序”,“我们可以传递一个向量,将案例按照它们在每个轴上的ID顺序排列”,并且可以在stat_alluvium()命令中传递向量。然而,在上面的代码中添加以下命令(分配顺序:lode_ord <- 1:28并将stat_alluvium(aes(order = lode_ord))添加到上面的代码中,会使图表混乱不堪 - 它会插入灰色线,减少流的颜色... 简直可怕。

有什么想法可以解决1)修复文本问题和2)以使黄色线始终堆叠在紫色线上的方式组织线条,以用于每个块的每个部分?

英文:

I have made an alluvia plot using the ggalluvial package in R. I am trying to order the flows (lines between blocks) so that they are much easier to read - I am aiming for them to be stacked on top of each other (orange color, followed by purple color) in each section for each block. Here I am defining a block to be the vertical bars that sit on the x-axis in positions 1, 2, and 3. Additionally, it is placing the text in inappropriate places within each block.

Reproducible Example

#Making the data.frame

w &lt;- c(&quot;Rain Forest&quot;, &quot;Desert&quot;, &quot;Arctic&quot;, &quot;City&quot;, &quot;Low Earth&quot;, &quot;Boonies&quot;, &quot;Space&quot;)
x &lt;- 1:40
y &lt;- c(&quot;Bird&quot;, &quot;Tiger&quot;, &quot;Cat&quot;, &quot;Parrot&quot;, &quot;T-Rex&quot;, &quot;Dreadwing&quot;)

set.seed(0)
GenComp &lt;- data.frame(
  Location = sample(w, size = 28, replace = TRUE),
  Frequency = sample(x = x, size = 28, replace = TRUE),
  Animal = sample(x = y, size = 28, replace = TRUE),
  Gender = rep(c(&quot;Male&quot;, &quot;Female&quot;), 14))

GenComp &lt;- GenComp[order(GenComp$Gender, GenComp$Animal, GenComp$Location, decreasing = FALSE),]
row.names(GenComp) &lt;- 1:nrow(GenComp)

GenComp$Location &lt;- factor(GenComp$Location, levels = sort(unique(GenComp$Location)))
GenComp$Gender &lt;- factor(GenComp$Gender, levels = c(&quot;Female&quot;, &quot;Male&quot;))
GenComp$Animal &lt;- factor(GenComp$Animal, levels = sort(unique(GenComp$Animal)))
GenComp &lt;- GenComp[, c(4, 3, 1, 2)]

#Graphing

library(ggalluvial)
ggplot(GenComp,aes(y = Frequency,
                            axis1 = Gender, axis2 = Animal, axis3 = Location)) + 
  geom_alluvium(aes(fill = Gender), width = 1/8, knot.pos = 0, reverse = FALSE) +
  geom_stratum(alpha = 0.25, width = 1/8, reverse = FALSE) +
  geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum)), reverse = TRUE, size = 3)  +
  ggtitle(&quot;Distribution of Fauna&quot;) +
  scale_fill_manual(values = c(Female = &quot;#691869&quot;, Male = &quot;#EBBC55&quot;)) +
  guides(fill = &quot;none&quot;) 

Here is the graph:
ggalluvial: 如何按照数据框中的顺序对块之间的线条进行排序?

The red arrows point to the text that is being written where they are not supposed to be. The blue circles are an example of what I don't want - circles show a mix of yellow and purple lines. I would like to always keep the yellow lines on top and purple under the yellow for each section of each block.

Trying to make sense of things, I learned in this post Ordering of streams within each block that 'the order of the streams in alluvial()is controlled by the row order. This is why I have ordered the GenComp data.frame in the order I would like to see the alluvia graph GenComp &lt;- GenComp[order(GenComp$Gender, GenComp$Animal,GenComp$Location, decreasing = FALSE),] ; row.names(GenComp) &lt;- 1:nrow(GenComp).

However, in the ggplot2 version, even if I reorder the rows, I get the same plot. A work around was the implementation of the commands lode_favor and lode_order.

If this is the answer, how can I implement these commands with ggalluvia in the code above?

I also learned here: ggalluvial - let order of stratums follow dataset that "you can re-order the axes by converting your character vectors to factors and specifying the levels you want." Which is why I have the factor(..., levels = ()) specified in the above code. This also did not work.

I read here: The Order of the Rectangles under the section: "Manual lode ordering" that "we can pass a vector that puts the cases in the order of their IDs in the data at every axis" and the vector can be passed in the stat_alluvium() command. However, adding the following commands to the code above (assigning the order: lode_ord &lt;- 1:28and adding stat_alluvium(aes(order = lode_ord)) into the code above made a horrific mess of the graph - it inserted gray lines, reduced the color of the flows ... just horrid.

Any ideas on what I can do to 1) fix the text issue and 2) organize the lines in such a way that the yellow lines always stack on top of the purple lines for each section in each block?

答案1

得分: 1

你要查找的选项是 lode.guidance = "forward"

ggplot(GenComp, aes(y = Frequency,
                    axis1 = Gender, axis2 = Animal, axis3 = Location)) + 
  geom_alluvium(lode.guidance = "forward", aes(fill = Gender), width = 1/8, knot.pos = 0, reverse = FALSE) +
  geom_stratum(alpha = 0.25, width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), reverse = TRUE, size = 3)  +
  ggtitle("Distribution of Fauna") +
  scale_fill_manual(values = c(Female = "#691869", Male = "#EBBC55")) +
  guides(fill = "none") 

ggalluvial: 如何按照数据框中的顺序对块之间的线条进行排序?

英文:

The option you're looking for is lode.guidance = &quot;forward&quot;:

ggplot(GenComp,aes(y = Frequency,
                            axis1 = Gender, axis2 = Animal, axis3 = Location)) + 
  geom_alluvium(lode.guidance = &quot;forward&quot;, aes(fill = Gender), width = 1/8, knot.pos = 0, reverse = FALSE) +
  geom_stratum(alpha = 0.25, width = 1/8, reverse = FALSE) +
  geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum)), reverse = TRUE, size = 3)  +
  ggtitle(&quot;Distribution of Fauna&quot;) +
  scale_fill_manual(values = c(Female = &quot;#691869&quot;, Male = &quot;#EBBC55&quot;)) +
  guides(fill = &quot;none&quot;) 

ggalluvial: 如何按照数据框中的顺序对块之间的线条进行排序?

huangapple
  • 本文由 发表于 2023年6月26日 10:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定