将 geom_path 与 POSIxct 日期一起使用以使用 ggplot2 显示随时间变化的轨迹
Using geom_path with POSIxct dates to show trajectories over time with ggplot2
数据:
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 并没有这样:
但是我还不知道该怎么做。到目前为止,我的代码如下所示:
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()
我希望这些点根据处理方式呈现不同的颜色,即使它们是多面的,但不得不放弃它以将日期转换为数字。然而,情节仍然没有以正确的时间顺序显示轨迹。这可能是因为日期跨越两年。
所以最终我的问题是。我怎样才能让轨迹线跟随正确日期序列中的点,同时还可能根据时间梯度为这条线着色?
如有任何帮助,我们将不胜感激!
这里有两个问题:1) geom_path 按照数据在数据框中出现的顺序绘制数据,因此需要按时间顺序排序,以及 2) seg3
按日期分组,因此在取消分组之前很难按日期总体排序。
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()
数据:
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 并没有这样:
但是我还不知道该怎么做。到目前为止,我的代码如下所示:
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()
我希望这些点根据处理方式呈现不同的颜色,即使它们是多面的,但不得不放弃它以将日期转换为数字。然而,情节仍然没有以正确的时间顺序显示轨迹。这可能是因为日期跨越两年。
所以最终我的问题是。我怎样才能让轨迹线跟随正确日期序列中的点,同时还可能根据时间梯度为这条线着色?
如有任何帮助,我们将不胜感激!
这里有两个问题:1) geom_path 按照数据在数据框中出现的顺序绘制数据,因此需要按时间顺序排序,以及 2) seg3
按日期分组,因此在取消分组之前很难按日期总体排序。
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()