为什么不介绍=; quate;使用“转换”时工作在r绘图中过滤
我对r绘制的新手很陌生,所以我仍然习惯所有参数。我正在探索的一个主题是如何通过过滤创建下拉菜单事件。我发现了使用此处找到的IRIS数据集找到了这个有用的示例noreferrer”> https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596/2 ,这是我下面提供的相同代码,并进行了一些轻微的更改。我正在尝试如何更改“ setosa”和“ versicolor”直方图的注释和形状,同时仍然能够根据数据更改图形。但是,当我尝试在布局()中使用方法=“ update”时,注释/形状会更改,但是即使在“ setosa”和“ versicolor”之间切换时,数据也保持不变。有人知道为什么会发生这种情况以及如何解决吗?
这是可再现的代码。我希望能够在同时更改注释/形状的同时过滤与花样相对应的直方图图形(以创建下拉菜单)。
library(plotly)
datasets::iris
line_shape <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "black"))
line_shape2 <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "red"))
line_annotation <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'black'))
line_annotation2 <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'red'))
p <- iris %>%
plot_ly(
type = 'histogram',
x = ~Sepal.Length,
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 = "update",
args = list(list("transforms[0].value", unique(iris$Species)[1]),
list(shapes = list(line_shape, c()),
annotations = list(line_annotation, c()))),
label = unique(iris$Species)[1]),
list(method = "update",
args = list(list("transforms[0].value", unique(iris$Species)[2]),
list(shapes = list(c(), line_shape2),
annotations = list(c(), line_annotation2))),
label = unique(iris$Species)[2])
)
)
)
)
p
I'm fairly new to r plotly so I'm still getting accustomed to all the arguments. One topic that I'm exploring is how to create a dropdown menu event by filtering. I found this helpful example using the iris dataset found here https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596/2, it's the same code I provided below with some slight changes. I was experimenting with how I can change the annotations and shapes for the "Setosa" and "Versicolor" histograms respectively while still being able to change the graphic in terms of the data. However, when I tried to use method = "update" in layout(), the annotations/shapes changed, but the data remained the same even when I toggled between "Setosa" and "Versicolor." Does anybody know why this happens, and how to fix it?
Here is the reproducible code. I want to be able to filter the histogram graphics that correspond to the flower species (to create a dropdown menu) while changing their annotations/shapes simultaneously.
library(plotly)
datasets::iris
line_shape <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "black"))
line_shape2 <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "red"))
line_annotation <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'black'))
line_annotation2 <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'red'))
p <- iris %>%
plot_ly(
type = 'histogram',
x = ~Sepal.Length,
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 = "update",
args = list(list("transforms[0].value", unique(iris$Species)[1]),
list(shapes = list(line_shape, c()),
annotations = list(line_annotation, c()))),
label = unique(iris$Species)[1]),
list(method = "update",
args = list(list("transforms[0].value", unique(iris$Species)[2]),
list(shapes = list(c(), line_shape2),
annotations = list(c(), line_annotation2))),
label = unique(iris$Species)[2])
)
)
)
)
p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论