R中的两个变量之间的交互式散点图,而无需使用闪亮

发布于 2025-02-03 03:30:34 字数 990 浏览 2 评论 0原文

是否可以在R中的两个变量(用户从多个变量中选择)之间创建一个交互式散点图,而无需使用Shiny。一个示例是在这里使用Shiny。是否可以在不使用闪亮的情况下制作这种交互式图?(例如,使用rmarkDown)

在这里使用一个按钮使用plotly上的按钮选择一个变量:

library(plotly)
df <- data.frame(x = 1:5, y = 7:11, z = 10:14)

create_buttons <- function(df, y_axis_var_names) {
  lapply(
y_axis_var_names,
FUN = function(var_name, df) {
  button <- list(
    method = 'restyle',
    args = list('y'
                , list(df[, var_name]))
    ,        label = sprintf('Show %s', var_name)
  )
},
df
  )

}

y_axis_var_names <- colnames(df)

p <- plot_ly(df, x = ~x, y = ~y, mode = "markers", name = "A", visible = T) %>%
  layout(
title = "Drop down menus - Styling",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
  list(
    y = 0.7,
    buttons = create_buttons(df, y_axis_var_names)
  )
))
p

但是我不知道如何在第二个按钮添加第二个按钮多变的!

Is it possible to create an interactive scatter plot between two variables (selected by user from multiple variables) in R, without using shiny. An example is here using shiny. Is it possible to make such interactive plot without using shiny?(For example using Rmarkdown)

An example is here that use one button to select a variable using a button on plotly:

library(plotly)
df <- data.frame(x = 1:5, y = 7:11, z = 10:14)

create_buttons <- function(df, y_axis_var_names) {
  lapply(
y_axis_var_names,
FUN = function(var_name, df) {
  button <- list(
    method = 'restyle',
    args = list('y'
                , list(df[, var_name]))
    ,        label = sprintf('Show %s', var_name)
  )
},
df
  )

}

y_axis_var_names <- colnames(df)

p <- plot_ly(df, x = ~x, y = ~y, mode = "markers", name = "A", visible = T) %>%
  layout(
title = "Drop down menus - Styling",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
  list(
    y = 0.7,
    buttons = create_buttons(df, y_axis_var_names)
  )
))
p

But I do not know how to add a second button to the second variable!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文