Skip to content

Commit

Permalink
Merge pull request #132 from NOAA-EDAB/andy_cfscale
Browse files Browse the repository at this point in the history
add opton to plot fahrenheit to spatial temperature maps
  • Loading branch information
sgaichas authored Feb 8, 2024
2 parents 6f9dd73 + 09a6e76 commit db2b1f6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 27 deletions.
45 changes: 36 additions & 9 deletions R/plot_bottom_temp_seasonal_gridded.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#'
#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot (most recent 10), passed from plot function
#' @param report Character string. Which SOE report ("MidAtlantic", "NewEngland"), passed from plot function
#'
#' @param scale character string. celsius or fahrenheit. Default = "celsius"
#'
#' @return ggplot object
#'
#' @export

plot_bottom_temp_seasonal_gridded <- function(shadedRegion = NULL,
report = "MidAtlantic") {
report = "MidAtlantic",
scale = "celsius") {


setup <- ecodata::plot_setup(shadedRegion = shadedRegion,
Expand Down Expand Up @@ -51,20 +52,45 @@ plot_bottom_temp_seasonal_gridded <- function(shadedRegion = NULL,
dplyr::mutate(Var = factor(Var, levels = c("winter","spring","summer","fall")))



if (scale == "fahrenheit") {
# convert celsius to fahrenheit
fix <- fix |>
dplyr::mutate(Value = (9/5)*Value + 32)
label <- "Temp. (\u00B0F)"
breaks <- c(41, 50, 59, 68, 77)
labelLegend <- c("41", "50", "59", "68", "77")
limits <- c(39,80)
midpoint <- 59
} else {
label <- "Temp. (\u00B0C)"
breaks <- c(5,10,15,20,25)
labelLegend <- c("5", "10", "15", "20", "25")
limits <- c(5,25)
midpoint <- 15
}

# fix <- fix |> dplyr::mutate(Value = replace(Value, Value > maxVal, maxVal))




p <- ggplot2::ggplot(data = fix)+
ggplot2::geom_tile(ggplot2::aes(x = Longitude, y = Latitude, fill = Value)) +
ggplot2::geom_sf(data = ecodata::coast, size = setup$map.lwd) +
ggplot2::geom_sf(data = ne_epu_sf, fill = "transparent", size = setup$map.lwd) +
ggplot2::coord_sf(xlim = xlims, ylim = ylims) +
ggplot2::facet_wrap(Var~.)+
ecodata::theme_map() +
#ggplot2::scale_fill_gradient2(name = 'Bottom \n Temp',
#low = scales::muted("blue"),
#mid = "white",
#high = scales::muted("red"),
#limits = c(5,25),
#labels = c("5", "10", "15", "20", "25")) +
ggplot2::scale_fill_viridis_c(option = 'B',name = 'Bottom \n Temp')+
#scales::show_col(viridis::inferno(n=3)) Find the colors
ggplot2::scale_fill_gradient2(name = label,
low = "#000004FF",
mid = "#BB3754FF",
high = "#FCFFA4FF",
limits = limits,
labels = labelLegend,
midpoint = midpoint) +
#ggplot2::scale_color_viridis_c(option = 'B',name = 'Bottom \n Temp')+
ggplot2::ggtitle('Seasonal Mean Bottom Temperature')+
ggplot2::xlab("Longitude") +
ggplot2::ylab("Latitude") +
Expand All @@ -76,6 +102,7 @@ plot_bottom_temp_seasonal_gridded <- function(shadedRegion = NULL,
axis.text = ggplot2::element_text(size = 8),
axis.title.y = ggplot2::element_text(angle = 90))+
ecodata::theme_title() +

ecodata::theme_ts()


Expand Down
38 changes: 30 additions & 8 deletions R/plot_ches_bay_sst.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot (most recent 10)
#' @param report Character string. Which SOE report ("MidAtlantic" only, default)
#' @param scale character string. celsius or fahrenheit. Default = "celsius"
#'
#' @return ggplot object
#'
Expand All @@ -12,7 +13,8 @@
#'

plot_ches_bay_sst <- function(shadedRegion = NULL,
report="MidAtlantic") {
report="MidAtlantic",
scale = "celsius") {

# generate plot setup list (same for all plot functions)
setup <- ecodata::plot_setup(shadedRegion = shadedRegion,
Expand Down Expand Up @@ -43,8 +45,26 @@ plot_ches_bay_sst <- function(shadedRegion = NULL,
"Summer",
"Fall"))

sst <- sst |> dplyr::mutate(Value = replace(Value, Value > 5, 5))
if (scale == "fahrenheit") {
# convert celsius to fahrenheit
sst <- sst |>
dplyr::mutate(Value = (9/5)*Value)
label <- "Temp.\nAnomaly (\u00B0F)"
breaks <- c(-9.0, -4.5, 0.0, 4.5, 9.0)
labelLegend <- c("<-9", "-4.5", "0", "4.5", ">9")
limits <- c(-9,9)
maxVal <- 9
midpoint <- 0
} else {
label <- "Temp.\nAnomaly (\u00B0C)"
breaks <- c(-5,-2.5,0,2.5,5)
labelLegend <- c("<-5", "-2.5", "0", "2.5", ">5")
limits <- c(-5,5)
maxVal <- 5
midpoint <- 0
}

sst <- sst |> dplyr::mutate(Value = replace(Value, Value > maxVal, maxVal))

# code for generating plot object p
# ensure that setup list objects are called as setup$...
Expand All @@ -54,15 +74,17 @@ plot_ches_bay_sst <- function(shadedRegion = NULL,
p <- sst |>
ggplot2::ggplot() +
ggplot2::geom_sf(data = ecodata::coast, size = setup$map.lwd) +
ggplot2::scale_fill_gradient2(name = "Temp.\nAnomaly (C)",
low = scales::muted("blue"),
mid = "white",
high = scales::muted("red"),
limits = c(-4,4),
labels = c("<-5", "-2.5", "0", "2.5", ">5")) +
ggplot2::coord_sf(crs = setup$crs, xlim = setup$xlims, ylim = setup$ylims) +

ggplot2::geom_tile(data = sst, ggplot2::aes(x = Latitude, y = Longitude,fill = Value)) +
ggplot2::scale_fill_gradient2(name = label,
low = scales::muted("blue"),
mid = "white",
high = scales::muted("red"),
limits = limits,
labels = labelLegend,
breaks = breaks,
midpoint = midpoint) +
ggplot2::facet_wrap(Var~.) +
ecodata::theme_map() +
ggplot2::ggtitle("Chesapeake Bay SST anomaly") +
Expand Down
39 changes: 32 additions & 7 deletions R/plot_seasonal_sst_anomaly_gridded.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#'
#' @param season Character string. Season to plot. (Default = NULL, plot all seasons)
#' @param region Character vector. Regional EPUs ("GB","MAB") to overly on figure. (Default = NULL, use all)
#' @param scale character string. celsius or fahrenheit. Default = "celsius"
#'
#'
#' @return ggplot object
#'
#' @export

plot_seasonal_sst_anomaly_gridded <- function(shadedRegion = NULL,
report = "MidAtlantic") {
report = "MidAtlantic",
scale = "celsius") {


setup <- ecodata::plot_setup(shadedRegion = shadedRegion,
Expand Down Expand Up @@ -47,8 +49,29 @@ plot_seasonal_sst_anomaly_gridded <- function(shadedRegion = NULL,
dplyr::mutate(Season = factor(Season, levels = c("Winter",
"Spring",
"Summer",
"Fall"))) |>
dplyr::mutate(Value = replace(Value, Value > 5, 5))
"Fall")))


if (scale == "fahrenheit") {
# convert celsius anomaly to fahrenheit anomaly
sst <- sst |>
dplyr::mutate(Value = (9/5)*Value )
label <- "Temp.\nAnomaly (\u00B0F)"
breaks <- c(-9.0, -4.5, 0.0, 4.5, 9.0)
labelLegend <- c("<-9", "-4.5", "0", "4.5", ">9")
limits <- c(-9,9)
maxVal <- 9
midpoint <- 0
} else {
label <- "Temp.\nAnomaly (\u00B0C)"
breaks <- c(-5,-2.5,0,2.5,5)
labelLegend <- c("<-5", "-2.5", "0", "2.5", ">5")
limits <- c(-5,5)
maxVal <- 5
midpoint <- 0
}

sst <- sst |> dplyr::mutate(Value = replace(Value, Value > maxVal, maxVal))


# filter by season
Expand All @@ -60,19 +83,21 @@ plot_seasonal_sst_anomaly_gridded <- function(shadedRegion = NULL,
ggplot2::geom_tile(data = sst, ggplot2::aes(x = Longitude, y = Latitude,fill = Value)) +
ggplot2::geom_sf(data = ecodata::coast, size = setup$map.lwd) +
ggplot2::geom_sf(data = ne_epu_sf, fill = "transparent", size = setup$map.lwd) +
ggplot2::scale_fill_gradient2(name = "Temp.\nAnomaly (C)",
ggplot2::scale_fill_gradient2(name = label,
low = scales::muted("blue"),
mid = "white",
high = scales::muted("red"),
limits = c(-5,5),
labels = c("<-5", "-2", "0", "2", ">5")) +
limits = limits,
labels = labelLegend,
breaks = breaks,
midpoint = midpoint) +
ggplot2::coord_sf(xlim = xlims, ylim = ylims) +
ggplot2::facet_wrap(Season~.) +
ecodata::theme_map() +
ggplot2::ggtitle("SST anomaly") +
ggplot2::xlab("Longitude") +
ggplot2::ylab("Latitude") +
ggplot2::theme(panel.border = ggplot2::element_rect(colour = "black", fill=NA, size=0.75),
ggplot2::theme(panel.border = ggplot2::element_rect(colour = "black", fill=NA, linewidth=0.75),
legend.key = ggplot2::element_blank(),
axis.title = ggplot2::element_text(size = 11),
strip.background = ggplot2::element_blank(),
Expand Down
8 changes: 7 additions & 1 deletion man/plot_bottom_temp_seasonal_gridded.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/plot_ches_bay_sst.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/plot_seasonal_sst_anomaly_gridded.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db2b1f6

Please sign in to comment.