tryCatch({ plots <-list()for (station_name inunique(cruise_df$station)) { subset_df <-filter(cruise_df, station == station_name) plots[[station_name]] <-ggplot(subset_df, aes(x = time_elapsed)) +geom_point(aes(y = depth), color ="blue") +# Plot depth in bluegeom_line(aes(y = sea_water_pressure), color ="red") +# Plot sea water pressure in redggtitle(glue("{station_name}")) +theme(axis.title.x =element_blank(),axis.title.y =element_blank(),axis.text.x =element_blank(),axis.text.y =element_blank(),axis.ticks.x =element_blank(),axis.ticks.y =element_blank() ) # Remove individual axis titles and text }# drop nulls plots <- purrr::compact(plots)# Combine all plots into a grid combined_plot <-wrap_plots(plots) +plot_layout(ncol =4) +# Adjust ncol to set number of columns in the gridplot_annotation(title ="Depth (blue) and Pressure (red)",subtitle ="Each panel represents a different station",caption ="Time Elapsed (x-axis) vs Depth & Pressure (y-axis)" ) +theme(plot.tag =element_text(size =12, face ="bold"),plot.tag.position ="topleft",axis.title.x =element_text(margin =margin(t =10)),axis.title.y =element_text(margin =margin(r =10)) )print(combined_plot)}, error =function(er){print(er)});
plot across all stations
p <-ggplot(cruise_df, aes(x = time, y = depth, fill = station)) +geom_col() +# This creates the bars# geom_text(aes(label = station), vjust = -0.3) + # This adds labels to each bar, adjust vjust for positionlabs(x ="Time", y ="Depth", title ="Depth over Time by Station") +# Set labels and titletheme_minimal() # Use a minimal themeprint(p)
create oce.ctd objects from dataframes
ctd_load <-function(data, other_params =NULL) {# create csv into ctd object test_ctd <-as.ctd(salinity = data$sea_water_salinity,temperature = data$sea_water_temperature,pressure = data$sea_water_pressure,station = data$station )# add additional columns to ctd objectif (!is.null(other_params)) {for (param_name in other_params) { test_ctd <-oceSetData(object = test_ctd,name = param_name,value = data[[param_name]] ) } }print(glue("{data$station[1]}:\t{length(test_ctd@data$scan)} scans"))return(test_ctd)}# Define other parameters to addother_params <-c("cruise_id", "station", "time", "time_elapsed", "latitude", "longitude", "sea_water_electrical_conductivity", "CDOM", "dissolved_oxygen","oxygen_saturation", "chlorophyll_concentration", "chlorophyll_fluorescence", "photosynthetically_available_radiation", "beam_attenuation","beam_transmission", "depth", "sea_water_sigma_t","descent_rate", "sound_velocity","altimeter")# Split data by station and create data listctd_FK <- cruise_df %>%split(.$station) %>%map(~ctd_load(.x, other_params = other_params)) # ~ is a lambda(x)
for (i inseq(ctd_FK)){ cast <- ctd_FK[[i]] # 1 is selecting only the first sublist# print(i)print(glue("=== station: {cast@metadata$station[1]}"))print(glue("# scans: {length(cast@data$scan)}"))plotScan(cast)}
=== station: .007
# scans: 3010
=== station: .009
# scans: 3990
=== station: .009_5
# scans: 6867
=== station: .010
# scans: 80
=== station: .012
# scans: 4670
=== station: .016
# scans: 2674
=== station: .018
# scans: 4197
=== station: .030
# scans: 3501
=== station: .031
# scans: 4094
=== station: .033
# scans: 3067
=== station: .033UP
# scans: 162
=== station: .041
# scans: 2591
=== station: .045
# scans: 3317
=== station: .047
# scans: 3069
=== station: .049
# scans: 3
=== station: .051
# scans: 3337
=== station: .053
# scans: 504
=== station: .054
# scans: 60
=== station: .055
# scans: 265
=== station: .056
# scans: 99
=== station: .057
# scans: 2488
=== station: .057_1
# scans: 2546
=== station: .057_2
# scans: 2865
=== station: .057_3
# scans: 3214
=== station: .058
# scans: 2890
=== station: .060
# scans: 3249
=== station: .064
# scans: 591
=== station: .065
# scans: 103
=== station: .068
# scans: 130
=== station: .21LK
# scans: 4542
=== station: .AMI1
# scans: 2926
=== station: .AMI2
# scans: 3628
=== station: .AMI3
# scans: 3573
=== station: .AMI4
# scans: 3916
=== station: .AMI5
# scans: 4675
=== station: .AMI6
# scans: 4195
=== station: .AMI7
# scans: 4299
=== station: .AMI8
# scans: 4530
=== station: .AMI9
# scans: 4565
=== station: .BG1
# scans: 3015
=== station: .BG2
# scans: 2915
=== station: .BG3
# scans: 3327
=== station: .BG4
# scans: 4291
=== station: .BG6
# scans: 4496
=== station: .BG7
# scans: 4337
=== station: .CAL1
# scans: 2674
=== station: .CAL2
# scans: 2756
=== station: .CAL3
# scans: 4071
=== station: .CAL4
# scans: 3697
=== station: .CAL5
# scans: 4402
=== station: .CAL6
# scans: 4194
=== station: .CW1
# scans: 2526
=== station: .CW2
# scans: 2823
=== station: .CW3
# scans: 3216
=== station: .CW4
# scans: 4230
=== station: .CW5
# scans: 4104
=== station: .CW6
# scans: 3571
=== station: .CW7
# scans: 4577
=== station: .GP5
# scans: 4477
=== station: .KW1
# scans: 3044
=== station: .KW2
# scans: 3640
=== station: .KW4
# scans: 3576
=== station: .L1
# scans: 5066
=== station: .L3
# scans: 3216
=== station: .L5
# scans: 3718
=== station: .L7
# scans: 2330
=== station: .L9
# scans: 4529
=== station: .MR
# scans: 4246
=== station: .ROME4
# scans: 3371
=== station: .ROME5
# scans: 2778
=== station: .RP1
# scans: 2948
=== station: .RP2
# scans: 3650
=== station: .RP3
# scans: 5145
=== station: .RP4
# scans: 4078
=== station: .TB1
# scans: 2541
=== station: .TB10
# scans: 4137
=== station: .TB2
# scans: 2901
=== station: .TB3
# scans: 3521
=== station: .TB4
# scans: 4027
=== station: .TB5
# scans: 4091
=== station: .V1
# scans: 3637
=== station: .V2
# scans: 3433
=== station: .V3
# scans: 3270
=== station: .V4
# scans: 4013
=== station: .V5
# scans: 3634
=== station: .V6
# scans: 5134
=== station: .V7
# scans: 4628
=== station: .V8
# scans: 4121
=== station: .V9
# scans: 4392
=== station: .WS
# scans: 3628
=== station: 002
# scans: 4282
plotting each cast in the first list
for (i inseq(ctd_FK)){ cast <- ctd_FK[[i]] # 1 is selecting only the first sublisttryCatch({plot(ctdDecimate(ctdTrim(cast))) }, error =function(e){print(e) })}
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3010' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3990' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 6867' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 80' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4670' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2674' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4197' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3501' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4094' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3067' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 162' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2591' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3317' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3069' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3337' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 504' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 99' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2488' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2546' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2865' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3214' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2890' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3249' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4542' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2926' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3628' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3573' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3916' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4675' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4195' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4299' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4530' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4565' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3015' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2915' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3327' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4291' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4496' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4337' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2674' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2756' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4071' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3697' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4402' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4194' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2526' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2823' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3216' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4230' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4104' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3571' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4577' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4477' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3044' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3640' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3576' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 5066' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3216' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3718' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2330' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4529' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4246' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3371' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2778' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2948' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3650' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 5145' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4078' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2541' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4137' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2901' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3521' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4027' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4091' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3637' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3433' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3270' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4013' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3634' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 5134' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4628' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4121' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4392' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3628' in coercion to 'logical(1)'>
<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4282' in coercion to 'logical(1)'>
plotting other physical parameters for each cast
# Loop through each CTD castfor (i inseq(ctd_FK)){ cast <- ctd_FK[[i]] # Assuming each sublist contains only one relevant CTD objecttryCatch({# Extract metadata for station name and cruise ID station_name <- cast@metadata$station[1] cruise_id <- cast@data$cruise_id[1]# Generate a title with station name and cruise ID overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")# Set margins: increase the outer margin for the titlepar(oma =c(0, 0, 3, 0)) # Top outer margin increased for title# Plotting function with specific parameters oce::plot(x =ctdDecimate(ctdTrim(cast)),which =c("sea_water_electrical_conductivity","descent_rate", "sound_velocity","sea_water_sigma_t","altimeter" ),main =""# No main title for individual subplots )# Place a single overall title at the top of the plot framemtext(overall_title, side =3, line =1, outer =TRUE, cex =1.5)# Reset outer margins to defaultpar(oma =c(0, 0, 0, 0)) }, error =function(e) {print(e$message) # Print any errors that occur during plotting })}
[1] "need finite 'xlim' values"
[1] "unknown xtype value (\"5.50915\")"
[1] "need finite 'xlim' values"
[1] "unknown xtype value (\"5.140198\")"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
plotting other nutrient parameters for each cast
# Loop through each CTD castfor (i inseq(ctd_FK)){ cast <- ctd_FK[[i]] # Assuming each sublist contains only one relevant CTD objecttryCatch({# Extract metadata for station name and cruise ID station_name <- cast@metadata$station[1] cruise_id <- cast@data$cruise_id[1]# Generate a title with station name and cruise ID overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")# Set margins: increase the outer margin for the titlepar(oma =c(0, 0, 3, 0)) # Top outer margin increased for title# Plotting function with specific parameters oce::plot(x =ctdDecimate(ctdTrim(cast)),which =c("CDOM", "dissolved_oxygen","oxygen_saturation","chlorophyll_concentration", "chlorophyll_fluorescence" ),main =""# No main title for individual subplots )# Place a single overall title at the top of the plot framemtext(overall_title, side =3, line =1, outer =TRUE, cex =1.5)# Reset outer margins to defaultpar(oma =c(0, 0, 0, 0)) }, error =function(e) {print(e$message) # Print any errors that occur during plotting })}
[1] "need finite 'xlim' values"
[1] "unknown xtype value (\"1.123\")"
[1] "unknown xtype value (\"259.9752\")"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
plotting other optical parameters for each cast
# Loop through each CTD castfor (i inseq(ctd_FK)){ cast <- ctd_FK[[i]] # Assuming each sublist contains only one relevant CTD objecttryCatch({# Extract metadata for station name and cruise ID station_name <- cast@metadata$station[1] cruise_id <- cast@data$cruise_id[1]# Generate a title with station name and cruise ID overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")# Set margins: increase the outer margin for the titlepar(oma =c(0, 0, 3, 0)) # Top outer margin increased for title# Plotting function with specific parameters oce::plot(x =ctdDecimate(ctdTrim(cast)),which =c("photosynthetically_available_radiation","beam_attenuation","beam_transmission" ),main =""# No main title for individual subplots )# Place a single overall title at the top of the plot framemtext(overall_title, side =3, line =1, outer =TRUE, cex =1.5)# Reset outer margins to defaultpar(oma =c(0, 0, 0, 0)) }, error =function(e) {print(e$message) # Print any errors that occur during plotting })}
[1] "unknown xtype value (\"0.098973\")"
[1] "unknown xtype value (\"0.099065\")"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
loop through every cast, clean, & save
combined_df <-data.frame()for (i inseq(ctd_FK)){tryCatch({ cast <- ctd_FK[[i]] # 1 is selecting only the first sublist# print(class(cast))# clean cast trimmed_cast <-ctdTrim(cast) decimated_cast <-ctdDecimate(trimmed_cast, p =0.5) # binned to 0.5 m# convert to df cast_df <-as.data.frame(decimated_cast@data)# Add metadata# assumes station ID and cruise ID the same for all & just uses 1st one cast_df <-mutate( cast_df,station = cast@data$station[1],cruise_id = cast@data$cruise_id[1] )# drop NA rows left by cleaning cast_df <-subset(cast_df, !is.na(scan))# Append the data to the combined dataframe combined_df <-rbind(combined_df, cast_df) }, error =function(e){print(glue("error in cast {cast@metadata$station[1]}"))print(e) })}# Save to CSVfile_path <-here(glue("data/cleaned/{cruise_id}.csv"))write.csv(combined_df, file_path, row.names =FALSE)