Skip to content

Commit

Permalink
enhance scatter2 plot
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinamullin committed Oct 26, 2023
1 parent 197f948 commit 24cd4bf
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions R/Figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
plot.data <- as.data.frame(.data)

# this subset must include all fields included in plot hover below
plot.data <- subset(plot.data, plot.data[, id_cols] %in% groups)[, c(id_cols, reqcols, depthcols, "ActivityStartDateTime", "MonitoringLocationName", "TADA.ActivityMediaName", "ActivityMediaSubdivisionName", "ActivityRelativeDepthName", "TADA.CharacteristicName")]
plot.data <- subset(plot.data, plot.data[, id_cols] %in% groups)[, c(id_cols, reqcols, depthcols, "ActivityStartDateTime", "MonitoringLocationName", "TADA.ActivityMediaName", "ActivityMediaSubdivisionName", "ActivityRelativeDepthName", "TADA.CharacteristicName", "TADA.MethodSpecificationName", "TADA.ResultSampleFractionText")]
plot.data$name <- gsub("_NA", "", plot.data[, id_cols])
plot.data$name <- gsub("_", " ", plot.data$name)

Expand All @@ -734,33 +734,39 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
param1 <- subset(plot.data, plot.data[, id_cols] %in% groups[1])
param2 <- subset(plot.data, plot.data[, id_cols] %in% groups[2])

title <- TADA::TADA_InsertBreaks(paste0("Scatterplot of ", param1$name[1], " and ", param2$name[1]), len = 40)
title <- TADA::TADA_InsertBreaks(paste0(param1$TADA.CharacteristicName[1],
" and ",
param2$TADA.CharacteristicName[1],
" Over Time"),
len = 45)

# figure margin
mrg <- list(
l = 50, r = 20,
b = 20, t = 55,
l = 50, r = 75,
b = 25, t = 75,
pad = 0
)

scatterplot <- plotly::plot_ly(type = "scatter", mode = "markers") %>%
plotly::layout(
xaxis = list(
title = "Activity Start Date",
#title = "Activity Start Date", # not necessary?
titlefont = list(size = 16, family = "Arial"),
tickfont = list(size = 16, family = "Arial"),
hoverformat = ",.4r", linecolor = "black", rangemode = "tozero",
showgrid = FALSE, tickcolor = "black"
),
yaxis = list(
title = param1$TADA.ResultMeasure.MeasureUnitCode[1],
title = paste0(param1$TADA.CharacteristicName[1], " ", param1$TADA.ResultMeasure.MeasureUnitCode[1]),
titlefont = list(size = 16, family = "Arial"),
tickfont = list(size = 16, family = "Arial"),
hoverformat = ",.4r", linecolor = "black", rangemode = "tozero",
showgrid = FALSE, tickcolor = "black"
),
yaxis2 = list(
side = "right", overlaying = "y", title = param2$TADA.ResultMeasure.MeasureUnitCode[1],
side = "right",
overlaying = "y",
title = paste0(param2$TADA.CharacteristicName[1], " ", param2$TADA.ResultMeasure.MeasureUnitCode[1]),
titlefont = list(size = 16, family = "Arial"),
tickfont = list(size = 16, family = "Arial"),
hoverformat = ",.4r", linecolor = "black", rangemode = "tozero",
Expand All @@ -769,15 +775,20 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
hoverlabel = list(bgcolor = "white"),
title = title,
plot_bgcolor = "#e5ecf6",
margin = mrg
margin = mrg,
legend = list(orientation = "h",
xanchor = "center",
x = 0.5)
) %>%
# config options https://plotly.com/r/configuration-options/
plotly::config(displaylogo = FALSE) %>% #, displayModeBar = TRUE) # TRUE makes bar always visible
plotly::add_trace(
data = param1,
x = ~ActivityStartDate,
y = ~TADA.ResultMeasureValue,
name = param1$TADA.CharacteristicName,
name = paste0(param1$TADA.ResultSampleFractionText, " ",
param1$TADA.CharacteristicName, " ",
param1$TADA.MethodSpecificationName),
marker = list(size = 10,
color = "#E34234",
line = list(color = "#005ea2", width = 2)),
Expand Down Expand Up @@ -812,7 +823,9 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
data = param2,
x = ~ActivityStartDate,
y = ~TADA.ResultMeasureValue,
name = param2$TADA.CharacteristicName,
name = paste0(param2$TADA.ResultSampleFractionText, " ",
param2$TADA.CharacteristicName, " ",
param2$TADA.MethodSpecificationName),
marker = list(size = 10, color = "#00bde3",
line = list(color = "#005ea2", width = 2)),
yaxis = "y2",
Expand Down

0 comments on commit 24cd4bf

Please sign in to comment.