有什么方法可以默认显示所有数据点而无需悬停?
Is there any way to display all datapoints by default without hovering?
需要找出是否有任何方法可以在开始时显示所有点而不需要悬停。根据所选图例,需要显示所有数据点。
library(plotly)
library(tidyr)
library(plyr)
library(htmlwidgets)
data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1')
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2')
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3')
fig <- fig %>% add_trace(y = ~Tree4, name = 'Tree 4')
fig <- fig %>% add_trace(y = ~Tree5, name = 'Tree 5')
fig <- fig %>% layout(legend = list(x = 0.1, y = 0.9))
fig
javascript <- "
var myPlot = document.getElementsByClassName('plotly')[0];
myPlot.on('plotly_hover', function(data) {
var hover = [];
for (var i = 0; i < data.points[0].data.x.length; i += 1) {
hover.push({curveNumber: data.points[0].curveNumber,
pointNumber: i});
}
Plotly.Fx.hover(myPlot, hover);
});"
fig <- htmlwidgets::prependContent(fig, onStaticRenderComplete(javascript), data = list(''))
fig
这是一个基于add_text(...)
的解决方案,不使用任何自定义 JS。您可能需要调整它以获得正确的外观和感觉。有必要使用 legendgroup
和 showlegend
参数来获得所需的行为。
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines+markers', name = 'Tree 1', legendgroup = 'Tree 1')
fig <- fig %>% add_text(y = ~Tree1, text = ~paste0("(", age, ", ", Tree1, ")"), legendgroup = 'Tree 1', showlegend=FALSE)
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2', legendgroup = 'Tree 2')
fig <- fig %>% add_text(y = ~Tree2, text = ~paste0("(", age, ", ", Tree2, ")"), legendgroup = 'Tree 2', showlegend=FALSE)
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3', legendgroup = 'Tree 3')
fig <- fig %>% add_text(y = ~Tree3, text = ~paste0("(", age, ", ", Tree3, ")"), legendgroup = 'Tree 3', showlegend=FALSE)
# etc for Trees 4 and 5
fig <- fig %>% layout(legend = list(x = 0.1, y = 0.9))
fig
library(plotly)
library(tidyr)
library(plyr)
library(htmlwidgets)
data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1')
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2')
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3')
fig <- fig %>% add_trace(y = ~Tree4, name = 'Tree 4')
fig <- fig %>% add_trace(y = ~Tree5, name = 'Tree 5')
fig <- fig %>% layout(legend = list(x = 0.1, y = 0.9))
fig
javascript <- "
var myPlot = document.getElementsByClassName('plotly')[0];
myPlot.on('plotly_hover', function(data) {
var hover = [];
for (var i = 0; i < data.points[0].data.x.length; i += 1) {
hover.push({curveNumber: data.points[0].curveNumber,
pointNumber: i});
}
Plotly.Fx.hover(myPlot, hover);
});"
fig <- htmlwidgets::prependContent(fig, onStaticRenderComplete(javascript), data = list(''))
fig
这是一个基于add_text(...)
的解决方案,不使用任何自定义 JS。您可能需要调整它以获得正确的外观和感觉。有必要使用 legendgroup
和 showlegend
参数来获得所需的行为。
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines+markers', name = 'Tree 1', legendgroup = 'Tree 1')
fig <- fig %>% add_text(y = ~Tree1, text = ~paste0("(", age, ", ", Tree1, ")"), legendgroup = 'Tree 1', showlegend=FALSE)
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2', legendgroup = 'Tree 2')
fig <- fig %>% add_text(y = ~Tree2, text = ~paste0("(", age, ", ", Tree2, ")"), legendgroup = 'Tree 2', showlegend=FALSE)
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3', legendgroup = 'Tree 3')
fig <- fig %>% add_text(y = ~Tree3, text = ~paste0("(", age, ", ", Tree3, ")"), legendgroup = 'Tree 3', showlegend=FALSE)
# etc for Trees 4 and 5
fig <- fig %>% layout(legend = list(x = 0.1, y = 0.9))
fig