Greetings,
I’m plotting a scatterplot with some “selected” points (coming from the “mask” column of a data source), but plot.ly seems to pull attributes (for color, size, etc.) from non-missing values. Here’s an example, which shouldn’t plot any red points. Can anyone else confirm this? Thanks.
if (!require('pacman')) install.packages('pacman')
pacman::p_load(
"dplyr",
"plotly"
)
#' iris dataset with NA values for Sepal.Width in the first rows after shuffling
#'
#' Note that only those rows with NA values for Sepal.Width
#' are selected by mask.
#'
#' @param rowsToNA integer, number of rows to assign NAs
#'
#' @return tibble with rowsToNA initial NA values for Sepal.Width
#' @export
#'
#' @examples
irisWithNA <- function(rowsToNA) {
base::set.seed(123)
dplyr::as_tibble(iris) %>%
dplyr::mutate(
key = base::as.character(dplyr::row_number())
) %>%
dplyr::arrange(stats::runif(dplyr::n())) %>%
dplyr::mutate(
Sepal.Width = base::ifelse(dplyr::row_number() <= rowsToNA, NA, Sepal.Width),
mask = base::is.na(Sepal.Width)
)
}
actual <- function(df) {
df %>%
plotly::plot_ly() %>%
plotly::add_trace(
type = "scatter",
mode = "markers",
x = ~Sepal.Length,
y = ~Sepal.Width,
text = ~base::paste0(Species, " (", key, ")"),
marker = list(
symbol = "o", # solid ball
color = ~base::ifelse(mask, "red", "black"),
size = ~10 * base::ifelse(mask, 1, 0.2),
line = list(width = 0L)
)
)
}
expected <- function(df) {
df %>%
plotly::plot_ly() %>%
plotly::add_trace(
type = "scatter",
mode = "markers",
x = ~Sepal.Length,
y = ~Sepal.Width,
text = ~base::paste0(Species, " (", key, ")"),
marker = list(
symbol = "o", # solid ball
color = "black",
size = 2,
line = list(width = 0L)
)
)
}
showResults <- function(rowsToNA = 10) {
# Because only NA rows are selected by the mask column,
# nothing in the plot should show up large and red.
# What appears to be happening is that the next non-NA
# values of the data frame are being plotted.
df <- irisWithNA(rowsToNA = rowsToNA)
plotly::subplot(actual(df), expected(df)) %>%
plotly::layout(
title = "Actual | Expected",
showlegend = FALSE
) %>%
print()
# Show the next non-NA values
print(head(df, 2 * rowsToNA))
}
showResults(rowsToNA = 1)