r - 在 R 中使用 plotly,更新形状和绘图的可见性
问题描述
我正在尝试使用按钮来更新形状和绘图。当我点击button1时,shape1和plot1出现;而 button2 将导致 shape2 和 plot2 出现。我尝试了更新和重新布局,但都没有成功。
任何帮助表示赞赏。
data1 <- runif(100,0,1000)
data2 <- runif(5,0,1000)
data3 <- runif(100,0,1000)
data4 <- runif(500,0,1000)
p <- plot_ly() %>%
add_trace(type = 'scatter', mode = 'markers',
y = data1, visible=T, marker = list(color = 'blue')) %>%
add_trace(type = 'scatter', mode = 'markers',
y = data2, visible=F, marker = list(color = 'red'))%>%
add_trace(type = 'scatter', mode = 'markers',
y = data3, visible=T, marker = list(color = 'gray')) %>%
add_trace(type = 'scatter', mode = 'markers',
y = data4, visible=F, marker = list(color = 'black')) %>%
layout(title ="Data12",
updatemenus = list(
list(type='buttons',
buttons = list(
list(method = "update",
args = list(list(visible=c(T, F,T,F)),list(title
="Data12"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
y0=0,y1=100, yref="y",fillcolor = "green",
line = list(color = "green")))),
label = 'data12'),
list(method = "update",
args = list(list(visible=c(F,T,F,T)),list(title
="Data34"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
y0=0,y1=100, yref="y",fillcolor =
"blue", line = list(color = "blue")))),
label = 'data34')
))))
p
解决方案
也许你可以试试这个:
library(plotly)
shapes12 = list(type="rect",x0=0,x1=100, xref="x",
y0=0,y1=100, yref="y",fillcolor = "green",
line = list(color = "green"))
shapes34 = list(type="rect",x0=0,x1=100, xref="x",
y0=0,y1=100, yref="y",fillcolor = "blue",
line = list(color = "blue"))
plot_ly() %>%
add_trace(type = 'scatter', mode = 'markers', name = "Data1",
y = data1, marker = list(color = 'blue')) %>%
add_trace(type = 'scatter', mode = 'markers', name = "Data2",
y = data2, marker = list(color = 'red'))%>%
add_trace(type = 'scatter', mode = 'markers', name = "Data3",
y = data3, marker = list(color = 'gray')) %>%
add_trace(type = 'scatter', mode = 'markers', name = "Data4",
y = data4, marker = list(color = 'black')) %>%
layout(updatemenus = list(
list(type='buttons',
buttons = list(
list(method = "update",
args = list(list(visible=c(T, T,F,F)),list(title ="Data12", shapes = list(shapes12))),
label = 'data12'),
list(method = "update",
args = list(list(visible=c(F,F,T,T)),list(title ="Data34", shapes = list(shapes34))),
label = 'data34')
)))
)
推荐阅读
- javascript - Jquery MultiSelect Dropdown 无法正确处理来自 Ajax 调用的数据
- ruby - 在 Windows 10 上安装 Ruby 2.3.2
- c# - 连接到 Azure 文件存储上的 SQLite 数据库
- javascript - 无法匹配任何路由。角度误差?
- python - 如何对列表中的元素进行分组?
- python - 如何在 Matplotlib 中使用 Latex 正确编写“<”和“>”符号
- android - 如何强制 FileBackupAgent 运行?
- swift - 尝试与活动视图控制器共享文件中的 json 字符串
- android - Cordova + Vue.js:在严格模式之外尚不支持块范围的声明(let、const、函数、类)
- python - 如何开发卷积神经网络来区分具有相似特征的图像?