简体   繁体   中英

Grouping elements in legend fails with 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.

Reproducible Example

# 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
)

Desired output

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

> plot

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

Why does this happen and how to prevent this? Thanks

使用 ggplot2 使用 ggplotly

EDIT: Appears to be related to Combined geom_bar and geom_point legend in ggplotly

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

pp <- ggplotly(
  p = plot
)

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM