R plotly's ggplotly removing layers from ggplot2's geom_point

62 Views Asked by At

I'm creating the following fill, color, and size coded plot in R's ggplot2's geom_point:

Example dataset:

library(dplyr)
library(ggplot2)

set.seed(1)
levels.mat <- matrix(data = runif(7*6, 1, 3), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))
scores.mat <- matrix(data = runif(7*6, 0, 10), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))
sig.mat <- matrix(data = sample(c(F, T), 7*6, replace = T), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))

df <- dplyr::left_join(reshape2::melt(levels.mat) %>% dplyr::rename(x = Var1, y = Var2, level = value),
                       reshape2::melt(scores.mat) %>% dplyr::rename(x = Var1, y = Var2, score = value)) %>%
  dplyr::left_join(reshape2::melt(sig.mat) %>% dplyr::rename(x = Var1, y = Var2, sig = value))

Plot:

ggplot(df, aes(x = x, y = y))+
  geom_point(aes(fill = level, size = score, color = sig),shape=21)+
  scale_fill_gradient(low = "lightgray", high = "darkred")+
  scale_color_manual(breaks = c(F, T), values = c("black", "green"))+
  theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Which gives: enter image description here

If I wrap it with plotly::ggplotly:

plotly::ggplotly(ggplot(df, aes(x = x, y = y))+
  geom_point(aes(fill = level, size = score, color = sig),shape=21)+
  scale_fill_gradient(low = "lightgray", high = "darkred")+
  scale_color_manual(breaks = c(F, T), values = c("black", "green"))+
  theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)))

I get: enter image description here

So the fill gradient seems to be gone and all points are filled with the same color.

I also get this warning message:

Warning messages:
1: In L$marker$color[idx] <- aes2plotly(data, params, "fill")[idx] :
  number of items to replace is not a multiple of replacement length
2: In L$marker$color[idx] <- aes2plotly(data, params, "fill")[idx] :
  number of items to replace is not a multiple of replacement length

Any idea how to fix this?

Here's my seesionInfo:

R version 4.2.3 (2023-03-15)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 14.1.2

other attached packages:
 [1] plotly_4.10.1.9000  rmarkdown_2.21      shinyjs_2.1.0       ggplot2_3.4.2       readr_2.1.4         DT_0.28             Matrix_1.5-4.1      dplyr_1.1.2        
 [9] shiny_1.7.4         BiocManager_1.30.20
0

There are 0 best solutions below