为多个时间序列图将数据帧与数据帧隔离 [英] Isolate Data Frame from Data Frame for Multiple Time Series Plot

查看:33
本文介绍了为多个时间序列图将数据帧与数据帧隔离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取特定压力水平(press_hpa)下的温度(temp_c)。当我使用dplyr过滤数据(dat)时,我正在创建另一个数据框,其中包含相同的列号(15)和不同的观察长度。有很多解决方案可以从列中绘制多个时间序列,但我无法匹配该解决方案。.如何绘制显示不同水平温度的多个时间序列(x =日期,y = temp_c,图例= Press_1000,Press_925,Press_850,Press_700 )?请帮助。.

I want to extract temperature (temp_c) at specific pressure level (press_hpa). As I am filtering my data (dat) using dplyr, I'm creating another data frame which contains the same columns numbers (15) and different length of observation. There were so many solution to plot multiple time series from column but I cant match the solution.. How to plot a multiple time series showing temperature at different level(x = date, y = temp_c, legend = Press_1000, Press_925, Press_850, Press_700)? Kindly help.. Thank you..

library(ggplot2), 
library(dplyr)
library(reshape2)

setwd("C:/Users/Hp/Documents/yr/climatology/")
dat <- read.csv("soundingWMKD.csv", head = TRUE, stringsAsFactors = F)

str(dat)
'data.frame':   6583 obs. of  15 variables:
 $ X        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ pres_hpa : num  1006 1000 993 981 1005 ...
 $ hght_m   : int  16 70 132 238 16 62 141 213 302 329 ...
 $ temp_c   : num  24 23.6 23.2 24.6 24.2 24.2 24 23.8 23.3 23.2 ...
 $ dwpt_c   : num  23.4 22.4 21.5 21.6 23.6 23.1 22.9 22.7 22 21.8 ...
 $ relh_pct : int  96 93 90 83 96 94 94 94 92 92 ...
 $ mixr_g_kg: num  18.4 17.4 16.6 16.9 18.6 ...
 $ drct_deg : int  0 0 NA NA 190 210 212 213 215 215 ...
 $ sknt_knot: int  0 0 NA NA 1 3 6 8 11 11 ...
 $ thta_k   : num  297 297 297 299 297 ...
 $ thte_k   : num  350 347 345 349 351 ...
 $ thtv_k   : num  300 300 300 302 300 ...
 $ date     : chr  "2017-11-02" "2017-11-02" "2017-11-02" "2017-11-02" ...
 $ from_hr  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ to_hr    : int  0 0 0 0 0 0 0 0 0 0 ...

Press_1000 <- filter(dat,dat$pres_hpa == 1000)
Press_925 <- filter(dat,dat$pres_hpa == 925)
Press_850 <- filter(dat,dat$pres_hpa == 850)
Press_700 <- filter(dat,dat$pres_hpa == 700)
date <- as.Date(dat$date, "%m-%d-%y")

str(Press_1000)
'data.frame':   80 obs. of  15 variables:
 $ X        : int  2 6 90 179 267 357 444 531 585 675 ...
 $ pres_hpa : num  1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 ...
 $ hght_m   : int  70 62 63 63 62 73 84 71 74 78 ...
 $ temp_c   : num  23.6 24.2 24.4 24.2 25.4 24 23.8 24 23.8 24 ...
 $ dwpt_c   : num  22.4 23.1 23.2 22.3 23.9 23.1 23.4 23 23 23.1 ...
 $ relh_pct : int  93 94 93 89 91 95 98 94 95 95 ...
 $ mixr_g_kg: num  17.4 18.2 18.3 17.3 19.1 ...
 $ drct_deg : int  0 210 240 210 210 340 205 290 315 0 ...
 $ sknt_knot: int  0 3 2 3 3 2 4 1 1 0 ...
 $ thta_k   : num  297 297 298 297 299 ...
 $ thte_k   : num  347 350 351 348 354 ...
 $ thtv_k   : num  300 301 301 300 302 ...
 $ date     : chr  "2017-11-02" "2017-11-03" "2017-11-04" "2017-11-05" ...
 $ from_hr  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ to_hr    : int  0 0 0 0 0 0 0 0 0 0 ...

 str(Press_925)
'data.frame':   79 obs. of  15 variables:
 $ X        : int  13 96 187 272 365 450 537 593 681 769 ...
 $ pres_hpa : num  925 925 925 925 925 925 925 925 925 925 ...
 $ hght_m   : int  745 747 746 748 757 764 757 758 763 781 ...
 $ temp_c   : num  21.8 22 22.4 23.2 22.2 20.6 22.4 22 22.4 22.2 ...
 $ ... 'truncated'

all_series = rbind(date,Press_1000,Press_925,Press_850,Press_700)

meltdf <- melt(all_series,id.vars ="date")
ggplot(meltdf,aes(x=date,y=value,colour=variable,group=variable)) + 
geom_line()


推荐答案

有两种解决方法。您追求的目标可能取决于基岩问题(我们不知道)。

There are two ways of approaching this. What you go for may depend on the bedrock question (which we don't know).

1)对于每个data.frame,您都有所有必要的列,并且可以使用例如

1) For each data.frame, you have all the necessary columns and you can plot each source (data.frame) using e.g.

ggplot()... +
    geom_line(data = Press_1000, aes(...)) +
    geom_line(data = Press_925, aes(...)) ...

请注意,您必须为每个颜色指定颜色

Note that you will have to specify color for each source and having a legend with that is PITA.

2)将所有data.frames合并为一个大对象,并创建一个附加列以指示数据的来源(从中数据来自观察)。这将是您当前 aes 调用中的映射变量(例如颜色,填充,组)。即时传奇。

2) Combine all data.frames into one big object and create an additional column indicating the origin of the data (from which data.frame the observation is from). This would be your mapping variable (e.g. color, fill, group)in your current aes call. Instant legend.

这篇关于为多个时间序列图将数据帧与数据帧隔离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆