用R中的na值绘制两个y轴图

时间:2017-03-06 15:02:32

标签: r ggplot2 data-visualization

data <- data.frame( Timestamp =c("2011-05-03 00:00:00 UTC", "2011-05-03 00:01:00 UTC", "2011-05-03 00:02:00 UTC",
 "2011-05-03 00:03:00 UTC", "2011-05-03 00:04:00 UTC", "2011-05-03 00:05:00 UTC",
  "2011-05-03 00:06:00 UTC", "2011-05-03 00:07:00 UTC", "2011-05-03 00:08:00 UTC",      "2011-05-03 00:09:00 UTC", "2011-05-03 00:10:00 UTC" ,"2011-05-03 00:11:00 UTC",
 "2011-05-03 00:12:00 UTC", "2011-05-03 00:13:00 UTC" ,"2011-05-03 00:14:00 UTC",
 "2011-05-03 00:15:00 UTC" ,"2011-05-03 00:16:00 UTC" ,"2011-05-03 00:17:00 UTC",
 "2011-05-03 00:18:00 UTC", "2011-05-03 00:19:00 UTC"  ), v1 =c(1:8,NA,20:25,NA,17:20), v2=c(1:4,NA,16:21,NA,23:28,NA,20),v3=c(1:10,21:30))

我有一个像上面这样的数据框架。我想在单个图中绘制“Timestamp vs v1,v2,v2”。如何用这些数据绘制折线图?

1 个答案:

答案 0 :(得分:1)

露丝,

如果我理解正确你想要的东西:

library(dplyr)
library(lubridate)

data %>%
  gather(Vgroup, value, -Timestamp) %>%
  mutate(Timestamp = ymd_hms(Timestamp)) %>% 
  na.omit() %>% 

ggplot(aes(Timestamp, value, colour=Vgroup)) +
  geom_point() +
  geom_line()

enter image description here