【问题标题】:Using geom_path with POSIxct dates to show trajectories over time with ggplot2使用带有 POSIxct 日期的 geom_path 来显示随着时间推移的轨迹与 ggplot2
【发布时间】:2019-05-18 01:29:41
【问题描述】:

数据:

segs3 <- structure(list(Date = structure(c(-62132918400, -62132918400, 
-62130499200, -62130499200, -62127820800, -62127820800, -62125228800, 
-62125228800, -62122550400, -62122550400, -62119958400, -62119958400, 
-62117280000, -62117280000, -62114601600, -62114601600, -62109331200, 
-62109331200, -62101382400, -62101382400, -62098963200, -62098963200, 
-62096284800, -62096284800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
    ), .Label = c("C", "T"), class = "factor"), cnmds1 = c(0.122961387545896, 
    0.057723977749837, 0.0300104088908616, -0.118427545586108, 
    0.232026011148594, 0.061587021296356, 0.385479649737433, 
    0.267544139583421, 0.221988530422909, -0.168855202757955, 
    0.0218318501737484, -0.231525498248828, 0.0160832637091355, 
    -0.186803075595128, -0.232613714047829, 0.0542629633219799, 
    -0.323422838323045, -0.213851711018165, -0.197755466321406, 
    -0.393692512349716, -0.0303311351612405, -0.015555599329904, 
    0.200994688464486, 0.263319025771876), cnmds2 = c(-0.206573078387224, 
    -0.0346956232380443, -0.0893959448563002, -0.0568011465358581, 
    -0.400917607471187, -0.632254641240973, -0.383454531095861, 
    -0.469614303049956, -0.215133320979806, -0.00834400437557489, 
    0.328182347160583, -0.0129823011324431, 0.350385587009896, 
    0.181878132786698, 0.667044860227797, 0.537754618186533, 
    0.327038282579616, 0.296924472706564, 0.54629597438437, 0.155846821010448, 
    -0.051982526318337, -0.075259505247973, -0.3519049986887, 
    -0.21313698658074)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -24L), groups = structure(list(
    `Date1[, 3]` = structure(c(-62132918400, -62130499200, -62127820800, 
    -62125228800, -62122550400, -62119958400, -62117280000, -62114601600, 
    -62109331200, -62101382400, -62098963200, -62096284800), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), .rows = list(1:2, 3:4, 5:6, 7:8, 
        9:10, 11:12, 13:14, 15:16, 17:18, 19:20, 21:22, 23:24)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

因此,我尝试使用来自两种不同治疗方法的 NMDS 分析中的一些点来绘制基于时间的轨迹。基本上,我想表明治疗 C 与治疗 T 相比,随着时间的推移有一个很好的圆形图案,而治疗 T 并没有:

但是我无法弄清楚如何做到这一点。到目前为止,我的代码如下所示:

ggplot(segs3, aes( x = cnmds1, y = cnmds2)) +
  geom_point(size = 4) + 
  geom_path(aes(color = as.numeric(Date))) +
  geom_line(arrow = arrow()) +
  facet_wrap(~Treatment) + 
  coord_fixed() 

我希望这些点是基于处理的不同颜色,即使它们是多面的,但必须放弃它才能将日期转换为数字。但是,该图仍未按正确的时间顺序显示轨迹。这可能是因为日期跨越两年。

所以最终我的问题是。如何让轨迹线跟随正确日期序列中的点,同时还可能根据时间梯度为这条线着色?

任何帮助将不胜感激!

【问题讨论】:

  • 使用您提供的df,返回相同的问题情节?

标签: r ggplot2 tidyverse lubridate


【解决方案1】:

这里有两个问题:1)geom_path按照数据在数据框中出现的顺序绘制数据,所以需要按时间顺序排序,2)seg3是按Date分组的,所以很难排序总体日期,直到您取消组合。

library(dplyr)
segs3 %>% 
  ungroup() %>%
  arrange(Date) %>%
ggplot(aes( x = cnmds1, y = cnmds2, color = Date)) +
  geom_point(size = 4) + 
  geom_path(arrow = arrow(type = "closed", 
                          length = unit(0.05, "npc"))) +
  facet_wrap(~Treatment) + 
  coord_fixed() 

【讨论】:

  • 谢谢!!快速提问。你怎么知道小标题是按日期分组的?我记得使用 group_by 创建 segs3,但如果我得到这些数据,人们怎么会发现第一步是 un_group?
  • 当我打印segs3 时,输出显示# A tibble: 24 x 4; # Groups: Date1[, 3] [12]。 (但首先当我尝试按日期安排时它没有表现......)
  • 知道了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2022-08-20
  • 2015-05-17
相关资源
最近更新 更多