'R CTD depth profile - how to create 1-m bins and average temp data for each bin

I am trying to create one figure with 3 subplots representing one year (e.g., 2018) of temperature, salinity, and fluorescence. The problem is the year has two casts but I want to combine the casts - aka, combine all the depth data into 1-m bins and then find the average temp, sal, and fluor to plot into 3 separate depth profiles. Additionally, I want to add the STD with a ribbon function. Here is my code:

#read in the data as two seperate data frames 
CTD18 <- read_excel("CTD_salpa combined.xlsx", sheet="2018_LTER")


#group by depth and find meal, STD for temp, sal,fluor
a1 <- CTD18 %>%
  group_by(CTDDEPTH) %>%
  summarise(mtemp = mean(temp),
            stemp = sd(temp))

a2 <- CTD18 %>%
  group_by(CTDDEPTH) %>%
  summarise(msal = mean(sal),
            ssal = sd(sal))

a3 <- CTD18 %>%
  group_by(CTDDEPTH) %>%
  summarise(mflu = mean(fluor),
            sflu = sd(fluor))

#temp subplot for 2018 CTD
plot1_18palmer<- ggplot(data=a1, aes(x=mtemp, y=CTDDEPTH))+
  geom_smooth(aes(x=mtemp,y = CTDDEPTH), size = 1) + 
  xlim(2.1,0.1)+
  geom_ribbon(aes(xmin = mtemp - stemp, xmax = mtemp + stemp),fill="black",
              alpha=0.3,
              linetype=1)+
  scale_y_reverse(lim=c(550,0))+
  scale_colour_continuous(high = "salmon", low = "navy")+
  xlab("Temperature (C)")+
  ylab("Depth (m)")+
  theme_classic()
 plot1_18palmer

enter image description here

Here is what I get from creating the subplot for temperature. Its not correct and I think its because I need to bin the data. Please help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source