数据:
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()
我希望这些点根据处理而具有不同的颜色,即使它们经过了多方面处理,但也不得不放弃以将Date转换为数字。但是,该图仍未按正确的时间顺序显示轨迹。可能是因为日期跨越两年。
所以最终我的问题是。如何使轨迹线遵循正确的日期序列中的点,同时还可能根据时间梯度为该线着色?
任何帮助将不胜感激!
答案 0 :(得分:2)
这里有两个问题: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()