R:将Plotly悬停模式设置为“比较悬停时的数据". [英] R: Set Plotly hovermode to "compare data on hover"
问题描述
我正在尝试使用Plotly软件包作为Shiny应用程序的一部分在R中创建堆积面积图,并希望比较悬停时的数据.但是,由于设计原因,我隐藏了模式栏,因此我需要在代码中声明此选项,因为当前仅将悬停显示为最接近光标的数据点.
I am trying to create a stacked area chart in R using the Plotly package as part of a Shiny app and would like to compare the data on hover. However, I am hiding the mode bar for design reasons, so I need to declare this option in my code as currently the hover is only shown for the closest data point to the cursor.
但是,用于R参考的图仅给出选项"x"(x上的工具提示-轴),"y"(y轴上的工具提示),最近"(显示与光标最近的数据点的工具提示)和FALSE(禁用工具提示).
However, the Plotly for R reference only gives the options "x" (tooltip on the x-axis), "y" (tooltip on the y axis), "closest" (shows the tooltip for the closest data point to the cursor) and FALSE (disables the tooltip).
有没有一种方法可以做我想做的事情?请注意,这个问题与这个问题完全相反.
Is there a way to do what I would like? Note that this question is pretty much the complete opposite of 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(的简化版本)为:
where a (simplified version of) data2 is:
Year A B
2006 18.0 82.0
2007 19.2 78.3
2008 17.9 80.2
2009 20.1 77.7
推荐答案
在您的代码中添加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的建议,可以使用:
EDIT
@OctavianCorlade sent me an important note about the solution given above: "The previously provided answer works, simply because any string different than the available options would produce the same result. hovermode = 'x'
is the documented way to do it, achieving the exact same result".
Hence, according to the suggestion of @OctavianCorlade, one can use:
layout(hovermode = 'x')
这篇关于R:将Plotly悬停模式设置为“比较悬停时的数据".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!