我们是否可以使这些值仅在悬停时可见。例如,只有在悬停时才能看到“丰田花冠”。现在所有的值都显示出来了
library(plotly)
data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10))
fig <- fig %>% add_annotations(x = data$wt,
y = data$mpg,
text = rownames(data),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5,
ax = 20,
ay = -40,
# Styling annotations' text:
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
fig
发布于 2021-11-28 21:22:50
要添加行名称/汽车类型,以便在将鼠标悬停在点上时显示它们,您只需添加text
并在hoverinfo
中使用它
在您的示例中,您可以使用rownames()
获取汽车类型并将它们添加到text
中,然后hoverinfo
可以使用这些类型
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10), text = rownames(data), hoverinfo = "text")
但是,如果您没有将汽车名称作为数据框中的行名和列,则可以将它们直接应用于hoverinfo
可使用的text
#Not required. Just used to create a column with car names#
data$cars <- rownames(data)
然后,您可以将新列用作text
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10), text = data$cars, hoverinfo = "text")
这两种方法都会给出以下结果
https://stackoverflow.com/questions/69933559
复制