【问题标题】:Is it possible to combine the ggplot legend with the plotly legend so they are equivalent?是否可以将 ggplot 图例与 plotly 图例结合起来,使它们等效?
【发布时间】:2020-04-20 16:15:50
【问题描述】:

我正在尝试将 ggplot 和 plotly 结合在一起来制作时间线。

效果很好,但使用图例时出现问题。下面的代码复制了我正在尝试做的事情。

library(ggplot2)
library(plotly)
x=1:10
df2 = data.frame(x,y = 2*x+rnorm(length(x)),lab = as.factor(c("col1","col2")))
status_colors <- c("#0070C0", "#00B050", "#FFC000", "#C00000","darkgreen","purple","darkgrey","blue","salmon","darkorange","black","navy","darkblue")
status_levels <- c(sort(unique(df2$lab))) 

p= ggplot(df2,aes(x=x, y=y, col = lab)) + geom_point() + labs(col="labtest") +
  scale_color_manual(values=status_colors,
                     labels=status_levels, drop = FALSE)

fig = ggplotly(p, tooltip = NULL)

fig %>%  
  add_text(
    x = df2$x,
    y = ifelse(df2$y>0,df2$y+0.05,df2$y-0.05),
    text = df2$lab,
    hovertext = df2$lab,
    hoverinfo = 'text',
    mode ="text",
    textfont = list(color=status_colors[df2$lab], size =10),
    marker = list(color=status_colors[df2$lab], size = 0.00001),
    showlegend = T,
    textposition = ifelse(df2$y>0,"top center","bottom center")
  )

基本上,正如您在图像中看到的那样,每个点的标签与它所附加的点颜色相同。但是,每当我从 plotly 添加标签文本的图例时,都会出现一个新的图例,它控制所有点,而不管它们的颜色如何。

因此,有没有一种方法可以将 ggplot 图例与 plotly 图例结合起来,以便只用正确的颜色编写 col1 和 col2 ,并且每当我与某种颜色的点交互时,附加到它的标签就会留在那里?

换句话说,有没有办法删除“trace 2”图例并让“add_text”知道在ggplot中已经创建了一个图例?

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    如果我说得对,除了摆脱第二个图例(可以通过设置showlegend = FALSE 轻松实现)之外,您还需要一个图例来控制点和标签。这可以通过legendgroups 来实现。而不是用一个add_text 添加标签,您可以(或必须?抱歉。仍然是一个阴谋新手,所以也许有更简单的方法)通过两个add_text 调用添加标签,每个col 调用一个。您可以通过 purrr::reduce 的魔力将这些添加到 ggplotly 对象,而不是复制和粘贴(这可能只适用于两个 cols,但有更多 cols ...)。试试这个:

    library(ggplot2)
    library(plotly)
    library(purrr)
    
    x=1:10
    df2 = data.frame(x,y = 2*x+rnorm(length(x)),lab = as.factor(c("col1","col2")))
    status_colors <- c("#0070C0", "#00B050", "#FFC000", "#C00000","darkgreen","purple","darkgrey","blue","salmon","darkorange","black","navy","darkblue")
    status_levels <- c(sort(unique(df2$lab))) 
    
    p= ggplot(df2,aes(x=x, y=y, col = lab)) + geom_point() + 
      labs(col="labtest") +
      scale_color_manual(values=status_colors,
                         labels=status_levels, drop = FALSE)
    
    fig = ggplotly(p, tooltip = NULL)
    
    purrr::reduce(c("col1", "col2"), ~ .x %>% add_text(
        data = filter(df2, lab == .y),
        x = ~x,
        y = ~ifelse(y > 0, y + 0.05, y-0.05),
        text = ~lab,
        hovertext = ~lab,
        hoverinfo = 'text',
        mode ="text",
        textfont = list(color= ~status_colors[lab], size =10),
        marker = list(color= ~status_colors[lab], size = 0.00001),
        showlegend = FALSE,
        textposition = ~ifelse(y>0, "top center","bottom center"),
        legendgroup = .y
      ), .init = fig) 
    

    顺便说一句:我还稍微简化了代码。您不需要df2$...,因为(gg)plotly 已经知道数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2020-12-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多