【问题标题】:Grouping elements in legend fails with ggplotlyggplotly 对图例中的元素进行分组失败
【发布时间】:2020-01-06 14:25:56
【问题描述】:

在以下示例中,分组 ggplot-elements 似乎有效,直到我使用 plotly::ggplotly() 转换绘图。我很难理解这是因为我做错了什么,还是这是另一个情节错误。

可重现的示例

# 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中的图例分组正确

&gt; plot

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

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

编辑:似乎与Combined geom_bar and geom_point legend in ggplotly相关

【问题讨论】:

标签: r ggplot2 plotly


【解决方案1】:

我查看了转换后的 plotly-object 并找到了一个临时(但丑陋的)修复:

pp <- ggplotly(
  p = plot
)

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

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
}

这可能是plotly中的一个bug,但由于缺乏替代方案,我会坚持这个解决方案。

【讨论】:

    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    相关资源
    最近更新 更多