4

I've made a plot (with ggplot geom_jitter) using three different subsets of the same dataframe:

  1. All rows
  2. Rows where pct.mito > 0.1
  3. Rows where pct.ribo > 0.3

enter image description here

I converted the ggplot object into a plotly object with ggplotly, with the intention of making each ggplot layer (each trace in the plotly object) selectable (while making the other two dim). However, this isn't the case and I can't figure out why, or how to enable the "on/off" feature for this plot. It usually is available on other plots I make using ggplot + ggplotly.

data <- diff.cells

p <- data %>% ggplot(aes(x = sample, y = nGene, 
                         label.1 = pct.mito, label.2 = pct.ribo, label.3 = nUMI)
                     ) +


# Layer 1: All rows:

geom_point(position = position_jitter(height = 0, width = .2), 
           size = .2, color = "grey50", alpha = 0.4) +

# geom_flat_violin(scale = "width", position = position_nudge(x = .3, y = 0), 
                 # colour = "#35a79c", fill = "#35a79c", alpha = 0.3) +



# Layer 2: pct.ribo >=  0.3 :  

   geom_point(data = data %>% filter(pct.ribo >= 0.3) %>% arrange(pct.ribo), 
           aes(colour = pct.ribo), 
           position = position_jitter(height = 0, width = .2), 
           size = .4, shape = 21, alpha = 0.7) +

  scale_colour_viridis_c(option = "viridis", begin = 0.25, end = 0.9) + 
                        #  limits = c(0.1, 0.5)) +


# Layer 3: pct.ribo >=  0.3

   geom_point(data = data %>% filter(pct.mito >= 0.1) %>% arrange(pct.mito), 
           aes(fill = pct.mito), 
           position = position_jitter(height = 0, width = .2), 
           shape = 21, stroke = 0, size = .8, alpha = 0.7) +

  scale_fill_viridis(option = "inferno", begin = 0.5, end = 0.9) +
                         # limits = c(0.1, 0.5)) + 

  # geom_boxplot(aes(x = as.numeric(sample) + 0.32, group=sample), 
  # colour = "grey50", width = .075, outlier.shape = NA, alpha = 0.3 ) +

  ylim(0, 4000) +

  theme_minimal() +
  theme(axis.text.x = element_text(angle = 30))


# Convert to plotly:

p <- ggplotly(p, tooltip = c("y", paste0("label.", 1:3)))

saveWidget(p, "diffCells.html")

The plotly object structure is located here:

plotly_json(p) %>% saveWidget("diffCells_plotly_json.html")

https://natalianutella.github.io/examples/diffCells_plotly_json.html

enter image description here

Thank you.

Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46

0 Answers0