Making a stacked bar plot based on ranges in R and plotly

前端 未结 2 1936
北海茫月
北海茫月 2021-01-29 06:51

I want to create a stacked bar chart in R and plotly using iris dataset. In the x-axis, I want to set limits like iris_limits below in the code and the y-axis should contain all

2条回答
  •  我在风中等你
    2021-01-29 07:31

    Try using cut:

    library(plotly)
    iris$iris_limits <- as.numeric(cut(iris$Sepal.Length,3))
    p <- plot_ly(iris, x = ~iris_limits, y = ~Sepal.Length, type = 'bar', name = 
                   'Sepal') %>%
      layout(yaxis = list(title = 'Count'), barmode = 'group')
    p
    

    The grouping details:

    > iris$Sepal.Length[iris$iris_limits==1]
     [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.4 5.1 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8
    [29] 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 5.5 4.9 5.2 5.0 5.5 5.5 5.4 5.5 5.5
    [57] 5.0 5.1 4.9
    > iris$Sepal.Length[iris$iris_limits==2]
     [1] 5.8 5.7 5.7 6.4 6.5 5.7 6.3 6.6 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.7 6.0 5.7 5.8 6.0
    [29] 6.0 6.7 6.3 5.6 6.1 5.8 5.6 5.7 5.7 6.2 5.7 6.3 5.8 6.3 6.5 6.7 6.5 6.4 5.7 5.8 6.4 6.5 6.0 5.6 6.3 6.7 6.2 6.1
    [57] 6.4 6.4 6.3 6.1 6.3 6.4 6.0 6.7 5.8 6.7 6.7 6.3 6.5 6.2 5.9
    > iris$Sepal.Length[iris$iris_limits==3]
     [1] 7.0 6.9 6.8 7.1 7.6 7.3 7.2 6.8 7.7 7.7 6.9 7.7 7.2 7.2 7.4 7.9 7.7 6.9 6.9 6.8
    > 
    

提交回复
热议问题