0
votes

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()
1

1 Answers

1
votes

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

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) 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.