相图,用垂直线连接离散的相或状态。

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

Phase diagram with vertical lines connecting discrete phases or states

问题

我使用 ggplot2 在 R 中绘制了一个状态图。离散的垂直轴显示了几个状态,水平轴显示时间。线条连接了一个阶段的起点和终点,不同的颜色表示不同的状态。这是一个示例:

  1. library("ggplot2")
  2. states <- data.frame(state = factor(c(2, 1, 2, 1)),
  3. start = c(0.5, 1.5, 3.5, 5.5),
  4. end = c(1.5, 3.5, 5.5, 7.5))
  5. ggplot(states, aes(x = start,
  6. xend = end,
  7. y = state,
  8. yend = state,
  9. color = state)) +
  10. geom_segment(size = 3)

如何连接一个阶段的终点与下一个阶段(不同状态)的起点,使用黑色垂直线?每个时间点都有且仅有一个状态,即没有重叠的状态和空时间点。

英文:

I draw a state diagram using ggplot2 in R. The discrete vertical axis shows several states, and the horizontal axis shows time. Lines connect the start and end points of a phase, with different colours per state. Here is an example:

  1. library(&quot;ggplot2&quot;)
  2. states &lt;- data.frame(state = factor(c(2, 1, 2, 1)),
  3. start = c(0.5, 1.5, 3.5, 5.5),
  4. end = c(1.5, 3.5, 5.5, 7.5))
  5. ggplot(states, aes(x = start,
  6. xend = end,
  7. y = state,
  8. yend = state,
  9. color = state)) +
  10. geom_segment(size = 3)

相图,用垂直线连接离散的相或状态。

How can I connect the end point of a phase with the start point of the next phase (in a different state) using a black, vertical line? Each time point has exactly one state, i.e., there are no overlapping states and no empty time points.

答案1

得分: 0

以下是翻译好的部分:

一种选择是将您的数据重塑为长格式,并使用 group 函数来绘制段以及通过 geom_line 函数绘制连接线:

  1. library(ggplot2)
  2. library(dplyr, warn=FALSE)
  3. library(tidyr)
  4. states1 <- states %>%
  5. mutate(id = row_number()) %>%
  6. pivot_longer(c(start, end))
  7. ggplot(states1, aes(
  8. x = value,
  9. y = state,
  10. color = state,
  11. )) +
  12. geom_line(aes(group = value),
  13. color = "black",
  14. linewidth = 3, lineend = "round"
  15. ) +
  16. geom_line(aes(group = id), linewidth = 3, lineend = "round")

相图,用垂直线连接离散的相或状态。

英文:

One option would be to reshape your data to long and use the group aes to draw the segments as well as the connecting lines via geom_line:

  1. library(ggplot2)
  2. library(dplyr, warn=FALSE)
  3. library(tidyr)
  4. states1 &lt;- states |&gt;
  5. mutate(id = row_number()) |&gt;
  6. pivot_longer(c(start, end))
  7. ggplot(states1, aes(
  8. x = value,
  9. y = state,
  10. color = state,
  11. )) +
  12. geom_line(aes(group = value),
  13. color = &quot;black&quot;,
  14. linewidth = 3, lineend = &quot;round&quot;
  15. ) +
  16. geom_line(aes(group = id), linewidth = 3, lineend = &quot;round&quot;)

相图,用垂直线连接离散的相或状态。

huangapple
  • 本文由 发表于 2023年7月14日 08:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683947.html
匿名

发表评论

匿名网友

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

确定