为使用ggplotly构建的多层绘图散点图启用迹线选择(突出显示特定迹线)

时间:2019-04-30 19:21:52

标签: r ggplot2 plotly r-plotly ggplotly

我已经使用同一数据帧的三个不同子集(使用ggplot geom_jitter进行了绘制:

  1. 所有行
  2. pct.mito > 0.1
  3. pct.ribo > 0.3

enter image description here

我将ggplot对象转换为ggplotly的plotly对象,目的是使每个ggplot层(plotly对象中的每条迹线)都是可选的(同时使其他两个变暗)。但是,事实并非如此,我无法弄清楚原因,或如何为该图启用“开/关”功能。通常可以在我使用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")

plotly对象结构位于此处:

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

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

enter image description here

谢谢。

0 个答案:

没有答案