简体   繁体   English

如何在不手动输入变量名称的情况下使用 plotly 制作 plot?

[英]How to make a plot using plotly without typing manually the name of the variables?

Is it possible to create a parallel coordinate plot using the 'plotly' package by only specifying the data frame so that all variables are used.是否可以通过仅指定数据框以使用所有变量来使用“plotly”package 创建平行坐标 plot。 All examples I can find specify the actual variables to be used in the plot by using the 'dimensions' option which seems very inefficient way to create the plot.我可以找到的所有示例都通过使用“维度”选项指定要在 plot 中使用的实际变量,这似乎是创建 plot 的低效方式。 What if the dataset has 20 or more variables and you don't want to type them every time.如果数据集有 20 个或更多变量并且您不想每次都键入它们怎么办。 Or it is a Shiny app that has to handle different user dataset with different variables.或者它是一个 Shiny 应用程序,它必须处理具有不同变量的不同用户数据集。

EXAMPLE3 uses the 'parcoords' package that needs only the data frame to create the plot.示例 3 使用“parcoords”package,它只需要数据帧即可创建 plot。 Is this possible to do with plotly?这可能与 plotly 有关吗?

Thank you!谢谢!

library(tidyverse)
library(plotly)

df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")


# EXAMPLE1 - This example works
fig1 <- df %>% plot_ly(type = 'parcoords',
                      line = list(color = ~species_id,
                                  colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue'))),
                      dimensions = list(
                        list(range = c(2,4.5),
                             label = 'Sepal Width', values = ~sepal_width),
                        list(range = c(4,8),
                             constraintrange = c(5,6),
                             label = 'Sepal Length', values = ~sepal_length),
                        list(range = c(0,2.5),
                             label = 'Petal Width', values = ~petal_width),
                        list(range = c(1,7),
                             label = 'Petal Length', values = ~petal_length)
                      )
)
fig1

# EXAMPLE2 - This example doesn't work
fig2 <- plot_ly(data = df,type = 'parcoords',
                      line = list(color = ~species_id,
                                  colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue')))
)
fig2


# EXAMPLE3 - Example using parcoords library
library(parcoords)
parcoords(df, brushMode = "1d-axes", reorderable = TRUE)



You can't omit the dimensions argument.您不能省略尺寸参数。

However, you can drastically reduce your typing effort by creating the dimensions-list programmatically via lapply .但是,您可以通过lapply以编程方式创建维度列表,从而大大减少您的打字工作量。 Please check the following:请检查以下内容:

library(plotly)

# df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")
df <- structure(list(sepal_length = c(5.1, 4.9, 4.7, 4.6, 5, 7, 6.4, 
6.9, 5.5, 6.5, 6.3, 5.8, 7.1, 6.3, 6.5), sepal_width = c(3.5, 
3, 3.2, 3.1, 3.6, 3.2, 3.2, 3.1, 2.3, 2.8, 3.3, 2.7, 3, 2.9, 
3), petal_length = c(1.4, 1.4, 1.3, 1.5, 1.4, 4.7, 4.5, 4.9, 
4, 4.6, 6, 5.1, 5.9, 5.6, 5.8), petal_width = c(0.2, 0.2, 0.2, 
0.2, 0.2, 1.4, 1.5, 1.5, 1.3, 1.5, 2.5, 1.9, 2.1, 1.8, 2.2), 
    species = c("setosa", "setosa", "setosa", "setosa", "setosa", 
    "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", 
    "virginica", "virginica", "virginica", "virginica", "virginica"
    ), species_id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L)), row.names = c(1L, 2L, 3L, 4L, 5L, 51L, 
52L, 53L, 54L, 55L, 101L, 102L, 103L, 104L, 105L), class = "data.frame")

dimensionColumns <- names(which(sapply(df, class) == "numeric")) # sapply(df, mode)

fig1 <- df %>% plot_ly(
  type = 'parcoords',
  line = list(color = ~ species_id,
              colorscale = list(c(0, 'red'), c(0.5, 'green'), c(1, 'blue'))),
  dimensions = lapply(dimensionColumns, function(x) {
    list(range = range(df[[x]]),
         label = x,
         values = as.formula(paste0("~", x)))
  })
)
fig1

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM