用ggplot绘制多个时间序列,其中包含交替的NA,反之亦然时间偏移?

时间:2018-03-22 18:46:45

标签: r plot ggplot2 na data-manipulation

我每次测量多个温度值。

 ## Harz 4
snow-temp 30
 snow_temp <- read_excel(path = "20180219_Harz-4_Tsnow.xlsx.xlsx", skip = 5)[,-1]
 weat_stat <- read_excel("20180219_Harz-4_T_RH.xlsx.xlsx", skip = 5)[,-1]
 head(weat_stat)
 A tibble: 6 x 6
  `Datum Zeit`        `Temp, 200cm, °C` `Temp - Avg, 200cm~ `RH, 200cm, %` `RH - Avg, 200cm,~ `DewPt, 200cm, ~
  <dttm>                          <dbl>               <dbl>          <dbl>               <dbl>            <dbl>
1 2018-02-17 16:22:14             -3.22               -3.52           84.8               84.8            -5.42
2 2018-02-17 16:32:14             -2.49               -3.22           90.5               89.4            -3.84
3 2018-02-17 16:42:14             -1.98               -2.41           88.3               89.7            -3.65
4 2018-02-17 16:52:14             -1.76               -1.93           88.3               88.6            -3.45
5 2018-02-17 17:02:14             -1.63               -1.76           86.8               88.1            -3.55
6 2018-02-17 17:12:14             -3.13               -2.02           89.6               87.2            -4.61
 head(snow_temp)
# A tibble: 6 x 5
  `Datum Zeit`        `T 117cm above ground` `T 100cm above ground` `T 50cm above ground` `T 0cm above ground`
  <dttm>                               <dbl>                  <dbl>                 
<dbl>                <dbl>
1 2018-02-17 16:30:00                  -3.75                 -0.704                -3.96                -4.02 
2 2018-02-17 16:40:00                  -4.05                 -1.16                 -3.90                -4.08 
3 2018-02-17 16:50:00                  -3.87                 -1.16                 -3.75                -3.81 
4 2018-02-17 17:00:00                  -3.93                 -1.78                 -3.84                -3.87 
5 2018-02-17 17:10:00                  -3.45                 -1.04                 -3.36                -3.51 
6 2018-02-17 17:20:00                  -3.04                 -2.68                 -0.760                0.135

如您所见,第二次测量的值被2:14偏移。所以我加入了一个大data.frame并按日期排序(不同的地点,同样的问题,因此不同的时间偏移):

> head(AlJoLiKi[,c(1,2,6)])
              Date_Time T_1 165 cm above ground Temp, °C, 215cm above ground
1   2018-02-17 17:30:00                  -3.896                           NA
266 2018-02-17 17:30:43                      NA                    -3.134502
2   2018-02-17 17:40:00                  -3.687                           NA
267 2018-02-17 17:40:43                      NA                    -4.035410
3   2018-02-17 17:50:00                  -3.598                           NA
268 2018-02-17 17:50:43                      NA                    -4.035410 

当我尝试用ggplot

绘制时
#AlJoLiKi melted
long.AlJoLiKi <- melt(AlJoLiKi[,1:6],id = "Date_Time")

ggplot(long.AlJoLiKi, aes(x = long.AlJoLiKi$Date_Time, color = variable, y = value)) +
  geom_line( size = 1) +

,我收到一条警告信息:

Warning message:
Removed 5 rows containing missing values (geom_path).

当绘制融化的data.frame时,使用正常图例绘制一个空的,完美缩放的绘图。

任何想法如何绘制这个,所以它只省略NAs并将所有温度值绘制为线条和x轴上的日期?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用gather中的tidyr制作一列位置。也就是说,

my_data <- data.frame("Date_Time" = c("2018-02-17 17:30:00", "2018-02-17 17:30:43",
                                      "2018-02-17 17:40:00", "2018-02-17 17:40:43",
                                      "2018-02-17 17:50:00", "2018-02-17 17:50:43"),
                      "temp.165" = c(-3.896, NA, -3.687, NA, -3.598, NA),
                      "temp.215" = c(NA, -3.1345, NA, -4.035, NA, -4.035), stringsAsFactors = F)
my_data$Date_Time <- as.POSIXct(my_data$Date_Time)

new_data <- tidyr::gather(my_data, "Location", "Temperature", 2:3)
new_data <- new_data[!is.na(new_data$Temperature), ]

ggplot2::ggplot(new_data, aes(x = Date_Time, color = Location, y = Temperature)) +
  geom_line( size = 1)