【问题标题】:Stacked area chart using Plotly and R without ggplot使用 Plotly 和 R 而不使用 ggplot 的堆积面积图
【发布时间】:2017-06-13 19:39:49
【问题描述】:

有没有办法只在 R 中使用 plot_ly 制作堆积条形图?我知道一个可能的解决方案是use ggplot and then convert with ggplotly,但它看起来不像其他情节图表那么好。 Plotly site 有一个示例,但是当通过单击图例删除类别时,总数保持不变。

制作示例数据:

library(tidyverse)
library(plotly)

# Create some data
grpnames <- c("Thing_3", "Thing_2", "Thing_1")
xval <- as.factor(c(100, 101, 102, 103))
frame <- merge(grpnames, xval, all=T)
yval <- runif(12, 0, .2)
df <- tbl_df(cbind(frame, yval))
colnames(df) <- c("GroupName", "X", "Y")
df.wide <- spread(df, key = GroupName, value = Y)

堆叠条有效:

# Creates a legit stacked bar where values sum to highest point
plot_ly(df, x = ~X, y = ~Y, color = ~GroupName, type='bar') %>% 
  layout(barmode = 'stack')

我找不到类似于折线图的“barmode = 'stack'”:

# Attempt with tidy data
df %>% 
  plot_ly(
    x = ~X, 
    y = ~Y, 
    color = ~GroupName, 
    type='scatter', 
    mode = 'lines', 
    fill = 'tonexty', 
    fillcolor = ~GroupName) 

这里尝试的 Plotly 方面的示例并没有为 X 的每个值添加 Y 的值——它只是覆盖它们。

# Attempt with wide data
df.wide %>% 
  plot_ly(
    x = ~X, 
    y = ~Thing_1, 
    name = 'Thing 1', 
    type = 'scatter', 
    mode = 'none', 
    fill = 'tozeroy', 
    fillcolor = 'aquamarine') %>% 
  add_trace(
    x = ~X, 
    y = ~Thing_2, 
    name = 'Thing 2', 
    fill = 'tonexty', 
    fillcolor = 'orange') %>% 
  add_trace(
    x = ~X, 
    y = ~Thing_3, 
    name = 'Thing 3', 
    fill = 'tonexty', 
    fillcolor = 'gray') 

有没有人能够成功地做到这一点?谢谢!

编辑澄清:我知道可以先做一个 cumsum 然后创建图表,但仍然感谢回复!我想知道是否可以在图表中进行求和,使其表现得像堆叠的条形图,单击图例删除组会显示剩余组的总和。

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    对于那些即使在几年后偶然发现这个问题的人(比如我自己):

    2021年@kkd42提到的第二个链接还是蛮有用的,解决方法在这里stackgroup='one'

    plot_ly(df,x=~X, y=~Y, color=~GroupName,
           type='scatter', mode='line', 
           stackgroup='one')
    

    为我做这项工作。

    【讨论】:

      【解决方案2】:

      您可以调整数据以使用该点的 y 值的累积和来计算堆叠值,例如

      library(plotly)
      library(tidyverse)
      
             # group, sort (to keep cumulative sum in right order), and adjust Y
      df %>% group_by(X) %>% arrange(GroupName) %>% mutate(Y = cumsum(Y)) %>% 
          plot_ly(type = 'scatter', x = ~X, y = ~Y, color = ~GroupName, 
                  mode = 'lines', fill = 'tonexty')
      

      【讨论】:

      • 感谢您的回答!如果可能的话,我希望堆叠/总和通过图表发生,以产生像堆叠条这样的解决方案,您可以在其中单击以删除一个组并查看剩余两个组的总和,但我可能会这样做,如果这是不可能的。
      • 公平;这种方法确实使图例交互变得毫无价值。
      【解决方案3】:

      您可以通过将要堆叠的东西加在一起来计算堆叠区域的高度。然后绘制这些已经堆积的累积值。原始问题中的“可重现”数据不可重现,因此我在这里用一些新数据进行演示。

      [注意plotly页面上示例中使用的数据也是这样转换成累积表的——https://plot.ly/r/filled-area-plots/#stacked-area-chart-with-cumulative-values]

      set.seed(123)
      df.wide  = data.frame(
        X = 100:105, 
        Thing_1 = cumsum(rnorm(6,10,3)), 
        Thing_2 = cumsum(rnorm(6,6,2)),
        Thing_3 = cumsum(rnorm(6,3,1)))
      
      df.wide$T1 = df.wide$Thing_1
      df.wide$T2 = df.wide$Thing_1 + df.wide$Thing_2
      df.wide$T3 = df.wide$T2 + df.wide$Thing_3
      
        plot_ly(df.wide, fill = 'tozeroy', line = list(color = '#00000000')) %>% 
          add_trace(x = ~X, y = ~T3, name = 'Thing 3', 
                    type = 'scatter', mode = 'lines',  fillcolor = 'green') %>% 
          add_trace(x = ~X, y = ~T2, name = 'Thing 2', 
                    type = 'scatter', mode = 'lines',  fill = 'tozeroy', fillcolor = 'blue') %>% 
          add_trace(x = ~X, y = ~T1, name = 'Thing 1', 
                    type = 'scatter', mode = 'lines',  fill = 'tozeroy', fillcolor = 'orange') 
      

      【讨论】:

      • 噢,对不起!编辑为实际上是一个独立的例子。 (并在新环境中确认)另外,感谢您的回答!我希望有一个解决方案可以产生类似于堆叠条形图的东西,不需要事先进行 cumsum,用户可以删除一个组并查看三个项目中任意两个的总和。
      猜你喜欢
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多