【发布时间】:2021-02-12 21:12:59
【问题描述】:
是否可以通过仅指定数据框来使用“plotly”包创建平行坐标图,以便使用所有变量。我可以找到的所有示例都通过使用“维度”选项指定要在绘图中使用的实际变量,这似乎是创建绘图的非常低效的方式。如果数据集有 20 个或更多变量并且您不想每次都键入它们怎么办。或者它是一个 Shiny 应用程序,它必须处理具有不同变量的不同用户数据集。
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)
【问题讨论】: