如何在不手动输入变量名称的情况下使用 plotly 绘图?
How to make a plot using plotly without typing manually the name of the variables?
是否可以通过仅指定数据框来使用“plotly”包创建平行坐标图,以便使用所有变量。我能找到的所有示例都通过使用“维度”选项来指定要在绘图中使用的实际变量,这似乎是创建绘图的非常低效的方法。如果数据集有 20 个或更多变量并且您不想每次都键入它们怎么办。或者它是一个闪亮的应用程序,必须处理具有不同变量的不同用户数据集。
EXAMPLE3 使用“parcoords”包,它只需要数据框来创建绘图。这可能与情节有关吗?
谢谢!
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)
您不能省略维度参数。
但是,您可以通过 lapply
以编程方式创建 dimensions-list,从而大大减少打字工作。请检查以下内容:
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
是否可以通过仅指定数据框来使用“plotly”包创建平行坐标图,以便使用所有变量。我能找到的所有示例都通过使用“维度”选项来指定要在绘图中使用的实际变量,这似乎是创建绘图的非常低效的方法。如果数据集有 20 个或更多变量并且您不想每次都键入它们怎么办。或者它是一个闪亮的应用程序,必须处理具有不同变量的不同用户数据集。
EXAMPLE3 使用“parcoords”包,它只需要数据框来创建绘图。这可能与情节有关吗?
谢谢!
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)
您不能省略维度参数。
但是,您可以通过 lapply
以编程方式创建 dimensions-list,从而大大减少打字工作。请检查以下内容:
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