图例中的元素分组因ggplotly而失败 [英] Grouping elements in legend fails with ggplotly

查看:98
本文介绍了图例中的元素分组因ggplotly而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,将ggplot-elements分组似乎可以正常工作,直到用plotly::ggplotly()转换图为止.我正在努力了解这是否是因为我做错了什么,或者这是另一个情节错误.

In the following example, grouping ggplot-elements appears to work, until I convert the plot with plotly::ggplotly(). I'm struggling to understand if this is because I'm doing something wrong, or if this is another plotly-bug.

# example data
df <- structure(list(a = structure(list(est_score = c(0.208979731611907,0.328919041901827, 0.396166493743658), upper_bound = c(0.992965325427929,1.11290463571785, 1.18015208755968), lower_bound = c(-0.575005862204114,-0.455066551914195, -0.387819100072363), ci_range = c(1.56797118763204,1.56797118763204, 1.56797118763204)), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame")), b = structure(list(est_score = c(0.688612399809063,0.584376397356391, 0.63451482411474), upper_bound = c(1.47259799362508,1.36836199117241, 1.41850041793076), lower_bound = c(-0.0953731940069589,-0.19960919645963, -0.149470769701281), ci_range = c(1.56797118763204,1.56797118763204, 1.56797118763204)), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame")), c = structure(list(est_score = c(0.462245718948543,0.636445740051568, 0.206650576367974), upper_bound = c(1.24623131276456,1.42043133386759, 0.990636170183996), lower_bound = c(-0.321739874867478,-0.147539853764454, -0.577335017448047), ci_range = c(1.56797118763204,1.56797118763204, 1.56797118763204)), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame")), d = structure(list(est_score = c(0.105384588986635,0.456747563555837, 0.281916436739266), upper_bound = c(0.889370182802657,1.24073315737186, 1.06590203055529), lower_bound = c(-0.678601004829386,-0.327238030260185, -0.502069157076755), ci_range = c(1.56797118763204,1.56797118763204, 1.56797118763204)), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame"))), row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame"))

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

y_names <- colnames(df) %>% unique()
y_n <- length(y_names)

plot_data <- df %>% mutate("case_id" = row_number()) %>% 
  tidyr::pivot_longer(
    cols = -case_id
  ) %>%
  arrange(name)


plot <- ggplot()

plot <- plot + geom_point(
  data = plot_data,
  aes(
    x = value$est_score,
    y = factor(name),
    shape = factor(case_id),
    color = factor(case_id)
  ),
  size = 2
)

plot <- plot + ggplot2::geom_segment(
  data = plot_data,
  aes(
    y = factor(name),
    yend = factor(name),
    x = value$lower_bound,
    xend = value$upper_bound,
    color = factor(case_id)
  ),
  size = .2
)

所需的输出

如您所见,ggplot中的图例已正确分组

Desired output

As you can see, the legend in ggplot is grouped correctly

> plot

ggplotly(p = plot)将三个geom_segments中的两个添加到图例中.

While ggplotly(p = plot) adds two of the three geom_segments to the legend.

为什么会发生这种情况以及如何防止这种情况发生? 谢谢

Why does this happen and how to prevent this? Thanks

编辑:似乎与推荐答案

我查看了转换后的plotly-object并找出了一个临时的(但很丑陋的)解决方法:

I took a look at the converted plotly-object and figured out a temporary (but ugly) fix:

pp <- ggplotly(
  p = plot
)

检查pp$x$data时,很明显,某些几何体的namelegendgroupshowlegend设置不同.这可以手动修复,例如通过遍历转换后的对象:

When examining pp$x$data, it becomes apparent that name, legendgroup and showlegend are set differently for some of the geoms. This can be fixed manually, e.g. by looping through the converted object:

n_cases <- length(unique(plot_data$case_id))
for (i in 1:n_cases) {
  pp$x$data[[i]]$name <- i
  pp$x$data[[i]]$legendgroup <- i
  pp$x$data[[i + n_cases]]$name <- i
  pp$x$data[[i + n_cases]]$legendgroup <- i
  pp$x$data[[i + n_cases]]$showlegend <- FALSE
}

这可能是密谋的错误,但是由于缺乏替代方案,我将坚持使用此解决方案.

This is probably a bug in plotly, but for the lack of alternatives, I will stick to this solution.

这篇关于图例中的元素分组因ggplotly而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆