我正在尝试使用Plotly软件包作为Shiny应用程序的一部分在R中创建堆积区域图表,并希望比较悬停时的数据.但是,由于设计原因,我隐藏了模式栏,因此我需要在我的代码中声明此选项,因为当前只显示最近数据指向光标的悬停.
然而,Plotly为R参考只给出了选项"X"(在x轴工具提示),"Y"(在y轴上工具提示),"最近的"(示出了最近的数据点,将光标的工具提示)和FALSE(禁用工具提示).
有办法做我想做的事吗?请注意,这个问题是相当多的完全相反这一块.
我正在使用的代码是:
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)
Run Code Online (Sandbox Code Playgroud)
其中a(简化版)data2是:
Year A B
2006 18.0 82.0
2007 19.2 78.3
2008 17.9 80.2
2009 20.1 77.7
Run Code Online (Sandbox Code Playgroud)
Mar*_*dri 12
添加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')
Run Code Online (Sandbox Code Playgroud)
编辑
@OctavianCorlade给我发了一个关于上面给出的解决方案的重要说明:" 之前提供的答案是有效的,因为任何字符串与可用选项不同都会产生相同的结果.hovermode = 'x'是记录的方法,实现完全相同的结果 " .
因此,根据@OctavianCorlade的建议,可以使用:
layout(hovermode = 'x')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3685 次 |
| 最近记录: |