0

我是这里的新手,很抱歉没有正确写出问题:p

如图 1 所示,目的是绘制关于我的研究地点(命名为 RB1)的一段时间内(从 2019 年 5 月到 2019 年 10 月选择 8 个日期)平均 NDVI 值的图表。并绘制垂直线以显示割草事件的日期。

2,现在我已经计算了这 8 个选定日期的 NDVI 值并制作了一个 CSV 文件。(PS.“切割”是指研究地点的草地已经被切割,所以对应的日期应该显示为垂直线,使用geom_vline)

infor <- read_csv("plotting information.csv")
infor
# A tibble: 142 x 3
   date         NDVI cutting
   <date>      <dbl> <lgl>  
 1 2019-05-12 NA     NA     
 2 2019-05-13 NA     NA     
 3 2019-05-14 NA     NA     
 4 2019-05-15 NA     NA     
 5 2019-05-16 NA     NA     
 6 2019-05-17  0.787 TRUE      
# ... with 132 more rows

3,问题是,当我做ggplot时,首先我想保持x轴为整个时间段(2019-05到2019-10)但当然不能显示其间的所有日期,否则会有办法x 轴上显示的日期过多)。所以,我scale_x_discrte(breaks=, labels=)用 NDVI 值来显示具体的日期。

其次,我还想显示割草的日期geom_vline

但是,似乎前提条件scale_x_discrtefactor我的日期,而前提条件geom_vline是保持日期为nummeric. 这两个呼吁似乎是矛盾的。

y1 <- ggplot(infor, aes(factor(date), NDVI, group = 1)) +
  geom_point() +
  geom_line(data=infor[!is.na(infor$NDVI),]) + 
  scale_x_discrete(breaks = c("2019-05-17", "2019-06-18", "2019-06-26", "2019-06-28","2019-07-23","2019-07-28", "2019-08-27","2019-08-30", "2019-09-21"), 
                   labels = c("0517","0618","0626","0628","0723","0728", "0827","0830","0921"))) 


y2 <- ggplot(infor, aes(date, NDVI, group = 1)) +
  geom_point() +
  geom_line(data=infor[!is.na(infor$NDVI),])) 

当我在 y1 中添加 geom_vline 时,我的绘图上不显示垂直线: y1 + geom_vline

当我在 y2 中添加它时,显示了垂直线,但日期(x 轴)很奇怪(不显示为 y1,因为我们不在这里运行 scale_x_) y2 + geom_vline

   y1 + 
      geom_vline(data=filter(infor,cutting == "TRUE"), aes(xintercept = as.numeric(date)), color = "red", linetype ="dashed")

如果您能提供帮助将不胜感激!提前致谢!:D

4

1 回答 1

0

我同意关于将日期保留为日期的评论。在这种情况下,您可以将 x 截距指定geom_vline为日期。

给定基本数据:

df <- tribble(
  ~Date, ~Volume, ~Cut,
  '1-1-2010', 123456, 'FALSE',
  '5-1-2010', 789012, 'TRUE',
  '9-1-2010', 5858585, 'TRUE',
  '12-31-2010', 2543425, 'FALSE'
)

我设置了日期,然后将子集拉到Cut=='TRUE'一个新对象中:

df <- mutate(df, Date = lubridate::mdy(Date))

d2 <- filter(df, Cut == 'TRUE') %>% pull(Date)

最后使用对象指定截距:

df %>%
  ggplot(aes(x = Date, y = Volume)) +
  geom_vline(xintercept = d2) +
  geom_line()
于 2019-11-20T01:18:05.430 回答