From 6376f2e756b1804909a931a8cc9134e118250642 Mon Sep 17 00:00:00 2001 From: Ryan Hafen Date: Fri, 22 Apr 2022 00:32:10 -0700 Subject: [PATCH] Add scatter annotations --- R/vis_scatter.R | 76 +++++++++++++++++++++++++++++++++++++++++++---- scripts/explore.R | 2 +- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/R/vis_scatter.R b/R/vis_scatter.R index 21f2588..50a8df8 100644 --- a/R/vis_scatter.R +++ b/R/vis_scatter.R @@ -38,6 +38,13 @@ scatter_vis_all <- function(rates) { maxrng <- max(alldat$pct) yrrng <- range(alldat$year) + nrows <- 3 + ncols <- ceiling(nrow(pdat) / nrows) + xpand <- function(rng, mult = 0.07) { + dff <- diff(rng) + c(rng[1] - mult * dff, rng[2] + mult * dff) + } + figs <- lapply(seq_len(nrow(pdat)), function(ii) { curr <- tidyr::nest(pdat$rdat[[ii]], data = -c("region")) curc <- pdat$cdat[[ii]] @@ -65,24 +72,26 @@ scatter_vis_all <- function(rates) { y = tmp$pct, color = I("darkgray"), line = list(width = 3), - size = 4, + size = 3, alpha = 0.6, name = paste(cur_cntry, cur_reg, sep = "___"), type = "scatter", mode = "lines+markers", hoverinfo = "text", - text = paste0(round(tmp$pct, 1), "%
Region: ", cur_reg), + text = paste0(round(tmp$pct, 1), + "% (", round(tmp$lower, 1), " \u2013 ", round(tmp$upper, 1), + ")
Country: ",cur_cntry, "
Region: ", cur_reg), showlegend = FALSE, name = paste(cur_cntry, cur_reg, sep = "___") ) %>% plotly::layout( yaxis = list( - range = list(0, maxrng), + range = xpand(c(0, maxrng)), zeroline = FALSE ), # TODO: determine fixed tickvals dynamically xaxis = list( - range = list(yrrng[1] - 1, yrrng[2] + 1), + range = xpand(c(yrrng[1], yrrng[2])), tickmode = "array", ticktext = c( "'00", @@ -111,8 +120,63 @@ scatter_vis_all <- function(rates) { fig }) - res <- plotly::subplot(figs, nrows = 2, shareY = TRUE, shareX = TRUE, - margin = 0.004) + annotations <- lapply(seq_len(nrow(pdat)), function(ii) { + cur_col <- (ii - 1) %% ncols + 1 + cur_row <- ceiling(ii / ncols) + list( + x = (1 / ncols) / 2 + (cur_col - 1) * (1 / ncols), + y = 1 - (cur_row - 1) * (1 / nrows), + text = as.character(pdat$country[ii]), + xref = "paper", + yref = "paper", + xanchor = "center", + yanchor = ifelse(cur_row == 1, "bottom", "top"), + showarrow = FALSE + ) + }) + + annotations <- c(annotations, list( + list( + text = "Survey Year", + x = 0.5, + y = 0, + showarrow = FALSE, + ax = 0, + ay = 0, + font = list( + size = 14.6118721461187 + ), + xref = "paper", + yref = "paper", + textangle = 0, + xanchor = "center", + yanchor = "top", + annotationType = "axis", + yshift = -19 + ), + list( + text = "STI Rate", + x = 0, + y = 0.5, + showarrow = FALSE, + ax = 0, + ay = 0, + font = list( + size = 14.6118721461187 + ), + xref = "paper", + yref = "paper", + textangle = -90, + xanchor = "right", + yanchor = "center", + annotationType = "axis", + xshift = -24 + ) + )) + + res <- plotly::subplot(figs, nrows = nrows, shareY = TRUE, shareX = TRUE, + margin = c(0.004, 0.004, 0.02, 0.02)) %>% + layout(annotations = annotations) res$elementId <- "scatterplot" diff --git a/scripts/explore.R b/scripts/explore.R index 2e5a97b..157118e 100644 --- a/scripts/explore.R +++ b/scripts/explore.R @@ -1,4 +1,4 @@ -library() +library(idhs) # load_all() # NOTE: before running this script run the code in data.R