r - 防止 ggplotly 从图例中删除未使用的因子水平

标签 r ggplot2 plotly

我有这个数据框:

> dd
   x xend         y     yend group
7  1    1 -2.592981 2.531884     C
8  2    2 -3.052151 2.570183     A
9  3    3 -3.255346 2.663808     C
10 4    4 -3.456388 2.905152     A
> str(dd)
'data.frame':   4 obs. of  5 variables:
 $ x    : num  1 2 3 4
 $ xend : num  1 2 3 4
 $ y    : num  -2.59 -3.05 -3.26 -3.46
 $ yend : num  2.53 2.57 2.66 2.91
 $ group: Factor w/ 3 levels "A","B","C": 3 1 3 1

我想绘制一些由 group 因子着色的段,而不从图例中删除未使用的因子级别。像这样:
gg <- ggplot(dd, aes(x, y, color=group)) +
   scale_colour_discrete(drop = FALSE) +
   geom_segment(aes(xend=xend, yend=yend))

这按预期工作:

ggplot

但现在我想要一个 plotly 图形。 ggplotly 删除未使用的级别:
ggplotly(gg)

enter image description here

有没有办法防止 ggplotly 删除未使用的因子水平?

数据框:
> dput(dd)
structure(list(x = c(1, 2, 3, 4), xend = c(1, 2, 3, 4), y = c(-2.59298110869713, 
-3.05215109954588, -3.25534630590118, -3.45638777944259), yend = c(2.53188374522142, 
2.57018254452851, 2.66380767012015, 2.90515183040407), group = structure(c(3L, 
1L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("x", 
"xend", "y", "yend", "group"), class = "data.frame", row.names = 7:10)

软件包版本:
> packageVersion("ggplot2")
[1] ‘2.2.1.9000’
> packageVersion("plotly")
[1] ‘4.6.0’

最佳答案

据我所知,没有直接的方法可以做到这一点,但您可以为每个缺少的 group 值添加一个“虚拟”行,例如

for (diff in setdiff(levels(dd$group), unique(dd$group))) {
  dummy_name = paste0('dummy_', diff) 
  dd[dummy_name,] <- dd[1,]
  for (n in names(dd[1,])) {
    dd[dummy_name,][n] = NaN
  }
  dd[dummy_name,]$group <- diff
}
这将给出以下数据框
          x xend         y     yend group
7         1    1 -2.592981 2.531884     C
8         2    2 -3.052151 2.570183     A
9         3    3 -3.255346 2.663808     C
10        4    4 -3.456388 2.905152     A
dummy_B NaN  NaN       NaN      NaN     B

和以下 plotly
enter image description here
dd = structure(list(x = c(1, 2, 3, 4), 
                    xend = c(1, 2, 3, 4), 
                    y = c(-2.59298110869713, -3.05215109954588, -3.25534630590118, -3.45638777944259), 
                    yend = c(2.53188374522142, 2.57018254452851, 2.66380767012015, 2.90515183040407), 
                    group = structure(c(3L, 1L, 3L, 1L), 
                                      .Label = c("A", "B", "C"), 
                                      class = "factor")), 
               .Names = c("x", "xend", "y", "yend", "group"), 
               class = "data.frame", 
               row.names = 7:10)
for (diff in setdiff(levels(dd$group), unique(dd$group))) {
  dummy_name = paste0('dummy_', diff) 
  dd[dummy_name,] <- dd[1,]
  for (n in names(dd[1,])) {
    dd[dummy_name,][n] = NaN
  }
  dd[dummy_name,]$group <- diff
}

gg <- ggplot(dd, aes(x, y, color=group)) +
  scale_colour_discrete(drop = FALSE) +
  geom_segment(aes(xend=xend, yend=yend))

gg
ggplotly(ggplot(dd, aes(x, y, color=group)) +
           scale_colour_discrete(drop = FALSE) +
           geom_segment(aes(xend=xend, yend=yend)))

关于r - 防止 ggplotly 从图例中删除未使用的因子水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684172/

相关文章:

python - Plotly:如何使用下拉菜单过滤 pandas 数据框?

R : convert a matrix to symmetric matrix with diagonal 0

r - ggplot : overlay two plots

r - 有没有办法计算 R 中多个因变量高于阈值的峰值数量?

r - 是否可以在 Power BI 的 R Script Visual 中使用 R Plotly 库?

python - 使用 Plotly 的 Slider 交互式绘图

r - 在ggplot2中为每个facet_wrap网格放置一个图例

r - 如何有效地将所有光栅文件导入 R?

r - 如何使用 R 在 Postgresql 中编写此 json 对象

r - ggplot2:如何在使用 scale_xx_manual 后创建正确的图例