【问题标题】:R Plotly how to use both position dodge and format hover textR Plotly如何同时使用位置闪避和格式悬停文本
【发布时间】:2021-04-13 16:18:53
【问题描述】:

我想创建一个绘图对象,它是 1.)在 x 轴上躲避的位置和 2.)按照我想要的方式格式化文本。我能够分别满足每个条件,但无法同时满足这两个条件。

在这里,我有一个适当位置躲避的情节图

library(tidyverse)
library(plotly)

df <- data.frame(
  x = c(1, 2, 3, 4),
  y = runif(8, 5, 10),
  cat = c("a", "a", "a", "a", "b", "b", "b", "b")
)

p <- ggplot(df, groups = cat) +
  geom_point(aes(x = x, y = y, color = cat),
             position = position_dodge(width = 0.3))

ggplotly(p)

在这里我有一个带有格式化悬停文本的情节

plot_ly(df, x = ~x, 
        y = ~y, 
        type = 'scatter', 
        mode = 'markers',
        color = ~cat,
        hoverinfo = 'text',
        text = ~paste('</br> X is ', x,
                      '</br> Y is ', y,
                      '</br> Cat is ', cat
        )
)

如何将这两个想法结合起来,以便我有一个位置躲避并带有手动格式化悬停文本的图?

【问题讨论】:

    标签: r ggplot2 plotly r-plotly


    【解决方案1】:

    您可以通过text“审美”传递您自定义的悬停文本,并将参数tooltip="text" 添加到您对ggplotly 的调用中:

    library(tidyverse)
    library(plotly)
    
    df <- data.frame(
      x = c(1, 2, 3, 4),
      y = runif(8, 5, 10),
      cat = c("a", "a", "a", "a", "b", "b", "b", "b")
    )
    
    p <- ggplot(df, groups = cat) +
      geom_point(aes(x = x, y = y, color = cat, 
                     text = paste('</br> X is ', x, '</br> Y is ', y, '</br> Cat is ', cat)), 
                 position = position_dodge(width = 0.3))
    
    ggplotly(p, tooltip = "text")
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2015-04-10
      • 2021-04-19
      • 1970-01-01
      • 2018-03-25
      • 2018-08-30
      • 2021-05-02
      • 2022-01-01
      • 2015-09-16
      相关资源
      最近更新 更多