代码之家  ›  专栏  ›  技术社区  ›  Prevost

R使用long data.frame或ggplotly的交互式堆叠面积图

  •  0
  • Prevost  · 技术社区  · 5 年前

    我正在从中重新创建交互式情节 plotly 下面的示例代码(已找到) here )但我想知道是否可以使用长data.frame格式来避免添加单个 add_trace 图例中每个变量的函数。类似 ggplot2 审美层次。

    我还从中创建了一个交互式堆叠面积图 ggplotly 但是,下面的交互功能不同。具体而言,当在图例处打开/关闭图层时,它们不会自行缩放,因此它们沿x轴是平的。它们看起来像是原样。例如,如果COLB是孤立的,它漂浮在情节的中间。

    阴谋地 上面的示例确实重置了图层,用户可以使用平面x轴参考目视检查各个图层的轮廓。

    谢谢你的帮助。

    library(plotly)
    
    data <- t(USPersonalExpenditure)
    data <- data.frame("year"=rownames(data), data)
    
    p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = '#F5FF8D') %>%
      add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
      add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
      add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
      add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
      layout(title = 'United States Personal Expenditures by Categories',
             xaxis = list(title = "",
                          showgrid = FALSE),
             yaxis = list(title = "Expenditures (in billions of dollars)",
                          showgrid = FALSE))
    
    p
    

    #

    library(data.table)
    library(magrittr)
    library(ggplot2)
    library(plotly)
    library(lubridate)
    
    dt <- data.table(colA = seq(from = ymd_hms("2020-01-01 00:00:00"),
                                to = ymd_hms("2020-01-01 00:00:00") + days(99),
                                by = "1 day"),
                     colB = runif(100,0,100),
                     colC = runif(100,0,100),
                     colD = runif(100,0,100)) %>% 
      melt(id.vars = "colA")
    
    ggplot <- ggplot(data = dt) +
      geom_area(aes(x = colA,
                    y = value,
                    fill = variable),
                stat = "identity",
                position = "stack",
                alpha = 0.5) +
      theme(legend.title = element_blank())
    ggplot
    
    ggplotly(ggplot)
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   kikoralston    5 年前

    @s\t给出的答案肯定有效。但为了完整性,我将添加另一种方法来实现这一点,它可能更干净。

    您还可以创建一个长数据帧并使用 split 争论 plot_ly

    plot_ly documentation :

    分裂 :(离散)用于创建多个记录道的值(每个值一个记录道)。

    # create data frame in long format
    data.long <- data %>% tidyr::pivot_longer(-year, names_to = "type", values_to = "value")
    
    # create plot_ly using split argument to separate traces according to type
    p <- plot_ly(data.long, x = ~year, y = ~value, type = 'scatter', 
                 mode = 'none', stackgroup = 'one', split = ~type)
    
    p
    

    enter image description here

        2
  •  1
  •   s__    5 年前

    library(plotly)
    
    # define your plot
    p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', 
                 type = 'scatter', mode = 'none', stackgroup = 'one')
    
    # select the columns you need to plot on the y axis: you remove the year (x axis) 
    # and the first one
    colstoplot <- setdiff(colnames(data),c("year","Food.and.Tobacco" ))
    
    # now you can loop through the columns in the vector colstoplot
    for(i in colstoplot){
      p <- p %>% add_trace(x = data[["year"]], y = data[[i]], name = i)
                           }
    # here the plot  
    p
    

    enter image description here

    推荐文章