简体   繁体   中英

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. 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. What if the dataset has 20 or more variables and you don't want to type them every time. Or it is a Shiny app that has to handle different user dataset with different variables.

EXAMPLE3 uses the 'parcoords' package that needs only the data frame to create the plot. Is this possible to do with 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 . 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

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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