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:
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:
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