2

考虑这个数据框:

data <- data.frame(ID = rep(1, 6),
                   Loc = c("A","B","D","A","D","B"),
                   TimeDiff = c(NA, 4.5,2.2,2.1,3.4,7.2))

我们在多个位置( )ID的观察结果相同。Loc观测值按它们发生的顺序排列,因此第一个观测值位于Loc == A,第二个观测值位于Loc == B,依此类推。TimeDiff是每次连续观察之间的时间段。我制作了以下图表以显示Loc随着时间的推移 s 之间的“路径”:

library(tidyverse)
data%>%
  mutate(RowNumber = row_number(), xend = lead(Loc), yend = lead(RowNumber))%>%
  ggplot()+
  geom_segment(aes(x = Loc, y = RowNumber, xend = xend, yend = yend), arrow = arrow(), size = 2)

在此处输入图像描述

我的主要问题:我们如何根据变量对每个箭头的大小进行加权TimeDiff,以及如何用相应的值标记每个箭头TimeDiff?表示连接前 2 个观测值的箭头在哪里Loc == ALoc == B将比后面的箭头更粗,因为TimeDiff两个观测值之间有一个更大的 (4.2)。

一个附带问题:请注意Locinclude AB和的 3 个级别D。假设C我想在B和之间的情节中包含另一个级别D。这怎么能扔进去?

4

2 回答 2

3

这是稍微修改数据的可能解决方案。缺点是硬编码nudge_xnudge_y

# modified data NA replaced by 0 and last value replaced by NA as we only have 5 differences in 6 datapoints

data <- data.frame(ID = rep(1, 6),
                   Loc = c("A","B","D","A","D","B"),
                   TimeDiff = c(0, 4.5,2.2,2.1,3.4,NA))

library(tidyverse)
data%>%
    mutate(RowNumber = row_number(), xend = lead(Loc), yend = lead(RowNumber))%>%
    ggplot()+
    geom_segment(aes(x = Loc, y = RowNumber, xend = xend, yend = yend), 
                 arrow = arrow(), size = data$TimeDiff) +
    geom_label(aes(x = Loc, y = RowNumber, xend = xend, yend = yend, label = data$TimeDiff),
               nudge_x = c(0.3, 0.5, -1, 1, -0.7),
               nudge_y = seq(0.2,6, 0.1))

在此处输入图像描述

于 2021-09-20T21:51:02.187 回答
1

嗯,不完全是一个漂亮的数字,但希望这足以让你开始。关键是放在size里面aes并使用scale_size_identity().

因此,在这种情况下size = 2,大小由 中的值控制,而不是所有段,TimeDiff例如4.52.2等。请注意,我将调用替换为替换,NA标签0替换为替换。size"NA"

library(tidyverse)

dat <- data.frame(ID = rep(1, 6),
                  Loc = c("A","B","D","A","D","B"),
                  TimeDiff = c(NA, 4.5,2.2,2.1,3.4,7.2)) %>%
  mutate(RowNumber = row_number(), 
         xend = lead(Loc), 
         yend = lead(RowNumber))


dat %>%
  ggplot(aes(x = Loc, y = RowNumber, xend = xend, yend = yend))+
  geom_segment(aes(size = replace_na(TimeDiff, 0)), arrow = arrow()) +
  geom_label(aes(label = replace_na(TimeDiff, "NA"))) +
  scale_size_identity()
#> Warning: Removed 1 rows containing missing values (geom_segment).

reprex 包于 2021-09-20 创建 (v2.0.0 )

于 2021-09-20T21:33:13.500 回答