R: Plot not Fully Loading

核能气质少年 提交于 2021-02-05 10:49:01

问题


I am working the R programming language. I am trying to follow this tutorial over here: https://plotly.com/r/parallel-coordinates-plot/

I am trying to make a "parallel coordinate plot" of the famous iris data set.

Instead of loading the iris data set through the github link, I tried to use the built in iris data set that is available in R:

#load library
library(plotly)

#load data
data(iris)
df = iris


#make plot
fig <- df %>% plot_ly(type = 'parcoords', line = list(color = ~Species, 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) ) )  

#view plot
fig

However, this produces an incomplete plot:

Does anyone know why these two plots are different and how can I fix this? Thanks


回答1:


The data used in the tutorial has an additional column species_id which is an integer column unique for every species. The default iris dataset does not have that so you can convert the Species column which is factor to integer.

library(plotly)

iris %>% plot_ly(type = 'parcoords', line = list(color = ~as.integer(Species), 
         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) ) )  



来源:https://stackoverflow.com/questions/65821992/r-plot-not-fully-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!