【问题标题】:R: Set Plotly hovermode to "compare data on hover"R:将 Plotly 悬停模式设置为“悬停时比较数据”
【发布时间】:2018-03-25 14:50:17
【问题描述】:

我正在尝试使用 Plotly 包作为 Shiny 应用程序的一部分在 R 中创建堆积面积图,并希望比较悬停时的数据。但是,出于设计原因,我隐藏了模式栏,因此我需要在我的代码中声明此选项,因为目前仅针对离光标最近的数据点显示悬停。

但是,Plotly for R reference 仅提供选项“x”(x 轴上的工具提示)、“y”(y 轴上的工具提示)、“closest”(显示最接近数据点的工具提示)光标)和 FALSE(禁用工具提示)。

有没有办法做我想做的事?请注意,这个问题与this one 几乎完全相反。

我使用的代码是:

plot_ly(data2, 
        x = ~Year,
        y = ~B, 
        name = 'In-centre', 
        type = 'scatter', 
        mode = 'none', 
        fill = 'tozeroy', 
        fillcolor = '#F5FF8D', 
        hoverinfo = 'y') %>%
add_trace(y = ~A, 
          name = 'At home', 
          fillcolor = '#50CB86', 
          hoverinfo = 'y') %>% 
layout(xaxis = list(title = "", 
                    showgrid = FALSE, 
                    tickangle = 270, 
                    dtick = 1, 
                    tickfont = list(size = 11)),
       yaxis = list(title = "", 
                    ticklen = 8, 
                    tickcolor = "#EEEEEE", 
                    range = c(-2, 101), 
                    tick0 = 0, 
                    dtick = 10, 
                    tickfont = list(size = 11)),
       showlegend = TRUE,
       legend = list(x = 0, 
                     y = -0.2, 
                     orientation = "h", 
                     traceorder = "normal"),
        margin = list(t = 25, b = 50, r = 10, l = 40)) %>% 
config(displayModeBar = FALSE)

data2 的(简化版)在哪里:

Year   A      B
2006   18.0   82.0
2007   19.2   78.3
2008   17.9   80.2
2009   20.1   77.7

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    layout(hovermode = 'compare') 添加到您的代码中:

    data2 <- read.table(text="
    Year   A      B
    2006   18.0   82.0
    2007   19.2   78.3
    2008   17.9   80.2
    2009   20.1   77.7
    ", header=T)
    
    library(plotly)
    library(dplyr)
    plot_ly(data2, 
            x = ~Year,
            y = ~B, 
            name = 'In-centre', 
            type = 'scatter', 
            mode = 'none', 
            fill = 'tozeroy', 
            fillcolor = '#F5FF8D', 
            hoverinfo = 'y') %>%
    add_trace(y = ~A, 
              name = 'At home', 
              fillcolor = '#50CB86', 
              hoverinfo = 'y') %>% 
    layout(xaxis = list(title = "", 
                        showgrid = FALSE, 
                        tickangle = 270, 
                        dtick = 1, 
                        tickfont = list(size = 11)),
           yaxis = list(title = "", 
                        ticklen = 8, 
                        tickcolor = "#EEEEEE", 
                        range = c(-2, 101), 
                        tick0 = 0, 
                        dtick = 10, 
                        tickfont = list(size = 11)),
           showlegend = TRUE,
           legend = list(x = 0, 
                         y = -0.2, 
                         orientation = "h", 
                         traceorder = "normal"),
            margin = list(t = 25, b = 50, r = 10, l = 40)) %>% 
    config(displayModeBar = FALSE) %>%
    layout(hovermode = 'compare')
    

    编辑 @OctavianCorlade 向我发送了关于上述解决方案的重要说明:“之前提供的答案有效,只是因为任何与可用选项不同的字符串都会产生相同的结果。hovermode = 'x' 是记录在案的方法,实现完全相同的结果”。
    因此,根据@OctavianCorlade 的建议,可以使用:

    layout(hovermode = 'x')
    

    【讨论】:

    • 现在看来 hovermode = 'x' 是强制性的。请编辑答案或添加 tl:dr 部分。
    猜你喜欢
    • 2015-04-10
    • 2018-08-30
    • 2021-04-19
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2018-02-11
    相关资源
    最近更新 更多