0

I like to produce similar graphs than the one produced by NOAA for their SST and Degree Heating Week (DHW)in R using ggplot (but if you have another way, I take it too)

NOAA graph for SST and DHW time series:

enter image description here

So I have one data frame per surveyed site that contains time series of Sea Surface Temperature (SST) and a different data frame with the DHW value for my study area.

Length of those data frames does not always match because time-series are not the same but they overlap.

SST dataframe look like this:
DateTime            meanSST DateShort
07/29/15 07:00:00 PM 26.781 07/29/15 
07/29/15 08:00:00 PM 27.272 07/29/15 
07/29/15 09:00:00 PM 25.708 07/29/15 
07/29/15 10:00:00 PM 25.902 07/29/15 
07/29/15 11:00:00 PM 25.805 07/29/15 
07/30/15 12:00:00 AM 25.610 07/30/15 

when DHW dataframe look like that:

Date2       SST_MIN SST_MAX DHWvalue
6/3/2013    26.683  29.8978 0
6/4/2013    26.9522 30.6074 0.168
6/5/2013    27.066  30.5716 0.342
6/6/2013    25.7735 30.6236 0.5282
6/7/2013    25.2618 30.4678 0.6848

I tried the following code in R:

#dataset
dd<-read.csv("HBH-LTER20150728-201610b.csv")
DHW<- read.csv("NOAA data South TW/DHW SouthernTW NOAA.csv")
#date format (lubridate required)
Sys.setlocale("LC_TIME", "usa") #windows
dd$Date = mdy(dd$Date)
DHW$Date2=mdy(DHW$Date2)

library(ggplot2)

#plot 
ggplot() + 
  geom_line(data = DHW, aes(x = Date2, y = DHWvalue, color = DHWvalue)) +
  scale_y_continuous(limits = c(0,31), breaks = seq(0,31,5)) +
  geom_line(data = dd, aes(x = Date, y = meanSST, color = meanSST) )+
  scale_colour_gradient2(low = "blue", mid = "light green" , high = "red", midpoint = 26)+
  scale_x_date(date_breaks = "2 month", date_labels = waiver())+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

and I obtain this figure:

enter image description here

My problem is that I only succeed to give a color gradient for the entire graph but I want one color gradient for my SST data (upper line) going from 20 to 31 degree and a second color gradient going from 0 to 15 for the DHW line, both starting from blue to their low values to dark red for the highest value. When I try to add a second scale_colour_gradient, my first gradient is replaced by the latest so I still failed to apply a different color gradient on my different lines.

Later on, I will also add SST for other sites from the area where I have a longer time series, that why I kept the entire time series for DHW.

any help will be extremely welcome. Sincerely, Lauriane

Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
  • Does this answer help? https://stackoverflow.com/questions/18347147/using-two-scale-colour-gradients-ggplot2 – Vincent Guillemot Aug 31 '18 at 08:59
  • maybe try geom_area for the DHW (you might need to interpolate to stop gaps in the fill) – Richard Telford Aug 31 '18 at 10:48
  • Thanks Vincent Guillemot, the post you referred me almost help. I believed I getting closer to what I want.the trick using geom_point is getting me closer but not yet the final results I want but I will try couple of more trick base on the post you refer me. – Lauriane ribas deulofeu Sep 01 '18 at 09:44
  • Richard Telford, I failed to implement gradient using geom_area for now. I'll try more option around the geom_area and ribbon. thanks – Lauriane ribas deulofeu Sep 01 '18 at 09:46

0 Answers0