[英]Control legend in ggplotly when using subplot
我使用R plotly
包和功能ggplotly
和subplot
以創建由多個單獨的交互的交互積ggplot2
圖。 一些圖共享相同的分組變量。
col <- factor(rep(c(1, 2), 5))
fill <- factor(c(rep("a", 5), rep("b", 5)))
x1 <- x2 <- y1 <- y2 <- 1:10
x3 <- y3 <- 11:20
d1 <- dplyr::tibble(x1 = x1, y1 = y1, col = col)
d2 <- dplyr::tibble(x2 = x2, y2 = y2, col = col, fill = fill)
d3 <- dplyr::tibble(x3 = x3, y3 = y3, col = col)
g1 <-
ggplot2::ggplot(d1) +
ggplot2::geom_point(ggplot2::aes(x = x1, y = y1, col = col))
g2 <-
ggplot2::ggplot(d2) +
ggplot2::geom_point(ggplot2::aes(x = x2, y = y2, col = col, fill = fill)) +
ggplot2::scale_fill_manual(values = c("red","green"))
g3 <-
ggplot2::ggplot(d3) +
ggplot2::geom_point(ggplot2::aes(x = x3, y = y3, col = col))
plotly::subplot(plotly::ggplotly(g1), plotly::ggplotly(g2), plotly::ggplotly(g3))
1)如何刪除交互式plotly
圖例中重復的“col”標簽?
2)如何刪除“fill”的圖例,但保留“col”的圖例?
編輯:我知道以下“臟”解決方案,即手動禁用圖例:
t <-
plotly::subplot(plotly::ggplotly(g1), plotly::ggplotly(g2), plotly::ggplotly(g3))
t$x$data[[1]]$showlegend <- FALSE
t$x$data[[2]]$showlegend <- FALSE
t$x$data[[3]]$showlegend <- FALSE
t$x$data[[4]]$showlegend <- FALSE
但是,這需要我提前知道列表元素的位置,這就是為什么我要尋找更通用的解決方案。
手動刪除不需要的圖例的另一種方法是使用style()
。 在您的示例中, lt <- t %>% style(t, showlegend = FALSE, traces = 3:n)
,其中n<-8
之前定義,將抑制不需要的圖例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.