Home > OS >  Issues plotting vertical profiles with depth (or elevation) on the y axis in R using ggplot2
Issues plotting vertical profiles with depth (or elevation) on the y axis in R using ggplot2

Time:02-05

I collected temperature and dissolved oxygen (DO) vertical profiles (from water surface to bottom) during my field surveys in a lake. I am trying to do some data exploration/viz to figure out how to use my data. I am trying to plot all my sites in the same plot by date, one plot for temperature (Temp_C) and one for dissolved oxygen (ODO_mgL) per survey date. I have two "Tows", one is S (start) and one E (end), I am only trying to plot the "S" tows for now.

I am having issues when plotting these, sometimes it comes out weird. I think it is because data was collected every second so there might be too many points. Is there a way I can plot this in a neater way? I would be okay with taking the mean every meter and having one temperature and one DO reading every meter instead of every second (i.e. 1 temp./DO reading at 1 m (average), 2m, 3m, 4m, 5m, etc...).

library(tidyverse)
library(ggplot2) 
    
sonde <- read.csv("SondeRaw/sonde_max_depth_no.up_byTow.csv",header=TRUE)
    
CH_720 <- sonde %>% 
filter(Date == '7/20/2021' & Tow == 'S')

#Temperature plot for all CH sites 07/20/2021
#This one looks good

ggplot(CH_720)   aes(x=Temp_C, y=Depth_m, color = Site)  
  geom_line()   scale_y_reverse()   xlim(0,25)  
  labs(title="Temperature", x="Temperature (C)", 
       y="Depth (m)", 
       caption="JUL 20 CH Sites")   
  theme(plot.title=element_text(hjust=0.5),
        plot.caption = element_text(face = "italic")) 

Temperature plot for all CH sites 07/20/2021 - this one looks good

#Dissolved oxygen plot for all CH sites 07/20/2021
#This one looks weird, particularly for site CH711 near the surface

ggplot(CH_720)   aes(x=ODO_mgL, y=Depth_m, color = Site)  
  geom_line()   scale_y_reverse()   xlim(0,11)  
  labs(title="Dissolved Oxygen", x="DO (mg/L)", 
       y="Depth (m)", 
       caption="JUL 20 CH Sites")   
  theme(plot.title=element_text(hjust=0.5),
        plot.caption = element_text(face = "italic")) 

Dissolved oxygen plot for all CH sites 07/20/2021 - this one doesnt look good

Access data here: enter image description here

Smoothing with a spline makes the trends clearer. I kept the plotting of the observations as points, but made them semitransparent (alpha = 0.33) to highlight the trend but without removing any information. I disabled the plotting of confidence band; you may want to add it. In geom_smooth() when using method="loess" you can play with the value passed to span to adjust how flexible the fitted spline is. I chose 0.3 by trial and error. You can make your on choice.

ggplot(CH_720)   aes(x=ODO_mgL, y=Depth_m, color = Site)  
  geom_point(alpha = 0.33)  
  geom_smooth(orientation = "y", method = "loess", span = 0.3, se = FALSE)  
  scale_y_reverse()  
  xlim(0,11)  
  labs(title="Dissolved Oxygen", x="DO (mg/L)",
       y="Depth (m)",
       caption="JUL 20 CH Sites")  
  theme(plot.title=element_text(hjust=0.5),
        plot.caption = element_text(face = "italic"))

As the trend lines overlap, I made a version with separate panels for the sites. (Splines, when gaps are present in the data sometimes make unexpected bends. To some extent this can be prevented by adjusting the span.)

ggplot(CH_720)   aes(x=ODO_mgL, y=Depth_m, color = Site)  
  geom_point(alpha = 0.33)  
  geom_smooth(orientation = "y", method = "loess", span = 0.3, se = FALSE)  
  scale_y_reverse()  
  xlim(0,11)  
  labs(title="Dissolved Oxygen", x="DO (mg/L)",
       y="Depth (m)",
       caption="JUL 20 CH Sites")  
  theme(plot.title=element_text(hjust=0.5),
        plot.caption = element_text(face = "italic"))  
  facet_wrap(~ Site)

enter image description here

Finally I used a logarithmic scale for DO. This, makes the patterns a bit cleaner, but maybe it is totally inapropriate for your data.

ggplot(CH_720)   aes(x=ODO_mgL, y=Depth_m, color = Site)  
  geom_point(alpha = 0.33)  
  geom_smooth(orientation = "y", method = "loess", span = 0.4, se = FALSE)  
  scale_y_reverse()  
  scale_x_log10(limits = c(1, 12))  
  #  xlim(0,11)  
  labs(title="Dissolved Oxygen", x="DO (mg/L)",
       y="Depth (m)",
       caption="JUL 20 CH Sites")  
  theme(plot.title=element_text(hjust=0.5),
        plot.caption = element_text(face = "italic"))  
  facet_wrap(~ Site)

enter image description here

  •  Tags:  
  • Related