如何告诉ggplot避免绘制丢失的日期

问题描述 投票:1回答:1

嗨,我有这样的数据:

> x
       value       Date
14 -2.224791 2000-01-31
15 -2.203189 2000-04-30
16 -2.216392 2000-07-31
17 -2.259517 2000-10-31
18 -2.252137 2001-01-31
19 -2.200599 2015-01-31
20 -2.229062 2015-04-30
21 -2.258825 2015-07-31
22 -2.288452 2015-10-31
> ggplot(x,aes(x=Date,y=value))+geom_line()

在试图绘图时,ggplot正在推断缺失日期,即2001年至2015年之间。我怎样才能告诉ggplot不绘制不可用的值? enter image description here

r ggplot2
1个回答
0
投票
library(tidyverse)

value = c(-2.224791, -2.203189, -2.216392, -2.259517, -2.252137, -2.200599, -2.229062, -2.258825, -2.288452)
date = as.Date(c("2000-01-31","2000-04-30","2000-07-31","2000-10-31","2001-01-31","2015-01-31","2015-04-30","2015-07-31","2015-10-31"))
data = data.frame(value,date)

data_2001 = data %>%
  filter(date <= "2001-12-31")

data_2015 = data %>%
  filter(date >= "2015-01-01")

plot = ggplot() +
  geom_line(data = data_2001
            , aes(x=date,y=value)) +
  geom_line(data = data_2015
            , aes(x=date,y=value))

print(plot)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.