8000 Add explicit aesthetic that maps to tooltip in {plotly}. by samussiah · Pull Request #584 · Gilead-BioStats/gsm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add explicit aesthetic that maps to tooltip in {plotly}. #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions R/Visualize_Count.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ Visualize_Count <- function(dfAnalyzed, strTotalCol = "N", strCountCol = "TotalC
"strTitle must be character" = is.character(strTitle)
)

p <- ggplot(
data = dfAnalyzed,
aes(x = reorder(.data$SiteID, -.data$N))
) +
# Define tooltip for use in plotly.
dfAnalyzedWithTooltip <- dfAnalyzed %>%
mutate(
tooltip = paste(
paste0('Site: ', .data$SiteID),
paste0('# of Events: ', format(.data$N, big.mark = ',', trim = TRUE)),
sep = '\n'
)
)

p <- dfAnalyzedWithTooltip %>%
ggplot(
aes(x = reorder(.data$SiteID, -.data$N), text = .data$tooltip)
) +
geom_bar(aes(y = .data[[strTotalCol]]), stat = "identity", color = "black", fill = "white") +
geom_bar(aes(y = .data[[strCountCol]]), stat = "identity", fill = "red") +
scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
ggtitle(strTitle) +
labs(
x = "Site ID",
Expand Down
35 changes: 23 additions & 12 deletions R/Visualize_Scatter.R
< 8000 tr data-hunk="60260826c7d910fc4cc17e2d2c350aad3fa9323009b812f8eb5f870500b2fbe1" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@
#' @export

Visualize_Scatter <- function(dfFlagged, dfBounds = NULL, strUnit = "days") {
# Define tooltip for use in plotly.
dfFlaggedWithTooltip <- dfFlagged %>%
mutate(
tooltip = paste(
paste0('Site: ', .data$SiteID),
paste0('Exposure (days): ', format(.data$TotalExposure, big.mark = ',', trim = TRUE)),
paste0('# of Events: ', format(.data$TotalCount, big.mark = ',', trim = TRUE)),
sep = '\n'
)
)

### Plot of data
p <- ggplot(
dfFlagged,
aes(
x = log(.data$TotalExposure),
y = .data$TotalCount,
color = as.factor(.data$Flag)
)
) +
p <- dfFlaggedWithTooltip %>%
ggplot(
aes(
x = log(.data$TotalExposure),
y = .data$TotalCount,
color = as.factor(.data$Flag),
text = .data$tooltip
)
) +
# Formatting
theme_bw() +
scale_x_continuous(
Expand All @@ -46,7 +57,7 @@ Visualize_Scatter <- function(dfFlagged, dfBounds = NULL, strUnit = "days") {
xlab(paste0("Site Total Exposure (", strUnit, " - log scale)")) +
ylab("Site Total Events") +
geom_text(
data = dfFlagged %>% filter(.data$Flag != 0),
data = dfFlaggedWithTooltip %>% filter(.data$Flag != 0),
aes(x = log(.data$TotalExposure), y = .data$TotalCount, label = .data$SiteID),
vjust = 1.5,
col = "red",
Expand All @@ -55,9 +66,9 @@ Visualize_Scatter <- function(dfFlagged, dfBounds = NULL, strUnit = "days") {

if (!is.null(dfBounds)) {
p <- p +
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$MeanCount), color = "red") +
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$LowerCount), color = "red", linetype = "dashed") +
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$UpperCount), color = "red", linetype = "dashed")
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$MeanCount), color = "red", inherit.aes = FALSE) +
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$LowerCount), color = "red", linetype = "dashed", inherit.aes = FALSE) +
geom_line(data = dfBounds, aes(x = .data$LogExposure, y = .data$UpperCount), color = "red", linetype = "dashed", inherit.aes = FALSE)
}

return(p)
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_Visualize_Count.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ test_that("incorrect inputs throw errors", {
expect_error(Visualize_Count(consent_assess$dfAnalyzed, strFlagCol = 1))
expect_error(Visualize_Count(consent_assess$dfAnalyzed, strTitle = list()))
})

test_that("Chart has [ text ] aesthetic", {
ie_assess <- IE_Assess(ieInput)
expect_true('text' %in% names(ie_assess$chart$mapping))
})
9 changes: 8 additions & 1 deletion tests/testthat/test_Visualize_Scatter.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
source(testthat::test_path("testdata/data.R"))

test_that("Output is produced", {

# poisson model
dfInput <- AE_Map_Adam(dfs = list(dfADSL = dfADSL, dfADAE = dfADAE))
SafetyAE <- AE_Assess(dfInput, strMethod = "poisson")
Expand All @@ -15,3 +14,11 @@ test_that("Output is produced", {
Visualize_Scatter(SafetyAE$dfFlagged)
expect_silent(Visualize_Scatter(SafetyAE$dfFlagged))
})

test_that("Chart has [ text ] aesthetic", {
dfInput <- AE_Map_Adam(dfs = list(dfADSL = dfADSL, dfADAE = dfADAE))
assessment <- AE_Assess(dfInput)
dfBounds <- Analyze_Poisson_PredictBounds(assessment$dfTransformed, c(-5, 5))
chart <- Visualize_Scatter(assessment$dfFlagged, dfBounds)
expect_true('text' %in% names(chart$mapping))
})
0