我使用的是R编程语言。我在这里遵循这个教程:
本教程声称要生成一个带有过滤器的交互式图形。然而,当我运行代码时,我没有看到任何过滤器。如果我做错了什么(或者我误解了代码),有人能告诉我吗?
library(plotly)
fig <- plot_ly(
type = 'scatter',
x = mtcars$hp,
y = mtcars$qsec,
text = rownames(mtcars),
hoverinfo = 'text',
mode = 'markers',
transforms = list(
list(
type = 'filter',
target = 'y',
operation = '>',
value = mean(mtcars$qsec)
)
)
)
fig此代码生成以下图形:

然而,似乎没有任何“过滤器”按钮。
如果我做错了什么,有人能告诉我吗?
谢谢
发布于 2021-02-09 01:06:19
实际上,代码中的过滤器只显示y>均值的数据点(mtcar$qsec)。我相信你正在寻找像下拉过滤器这样的东西,就像这里讨论的:https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596。
发布于 2021-02-09 02:45:13
以下代码来自https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596。(见上面@rodrigocfaria的回答):
library(plotly)
p <- iris %>%
plot_ly(
type = 'scatter',
x = ~Sepal.Length,
y = ~Petal.Length,
text = ~Species,
hoverinfo = 'text',
mode = 'markers',
transforms = list(
list(
type = 'filter',
target = ~Species,
operation = '=',
value = unique(iris$Species)[1]
)
)) %>% layout(
updatemenus = list(
list(
type = 'dropdown',
active = 0,
buttons = list(
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[1]),
label = unique(iris$Species)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[2]),
label = unique(iris$Species)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[3]),
label = unique(iris$Species)[3])
)
)
)
)
#view plot
p

https://stackoverflow.com/questions/66105723
复制相似问题