【问题标题】:R interactive stacked area chart using long data.frame or ggplotly使用 long data.frame 或 ggplotly 的 R 交互式堆积面积图
【发布时间】:2020-01-14 04:14:11
【问题描述】:

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

任何交互式绘图解决方案都可以使用(highcharter、plotly 等)。

我还从下面的ggplotly 创建了一个交互式堆积面积图,但是交互式功能不一样。具体来说,当图层在图例中打开/关闭时,它们不会自行缩放,因此它们沿 x 轴是平的。它们按原样显示。例如,如果 colB 是孤立的,它会漂浮在图的中间。

上面的plotly 示例确实重置了图层,用户可以使用平面 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)

【问题讨论】:

    标签: r ggplot2 r-plotly r-highcharter


    【解决方案1】:

    @s_t 给出的答案绝对有效。但为了完整起见,我将添加另一种可能更清洁的方法来完成此操作。

    您还可以创建一个长数据框并在plot_ly 中使用split 参数

    来自plot_ly documentation

    拆分:(离散)值用于创建多条轨迹(每个值一条轨迹)。

    这可能与@s_t 选项的作用相同。但它更干净一些。

    # 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
    

    【讨论】:

    • 我对@9​​87654327@ 不是很熟悉,但它(至少对我而言)显示为一个内置函数(或解决方法),以解决在长宽之间绘制时的数据格式差异数据。 :)
    • 您也可以删除split = ~type 并添加color = ~type, colors = c("black", "red", "blue", "green", "purple")...或任何颜色变化...效果相同。这种方式还可以让您控制映射到每条轨迹的颜色。
    【解决方案2】:

    你可以试试这样的:

    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
    

    【讨论】:

    • 这是个好主意...我打算用暴力破解它并在 Excel 中创建 add_trace 函数并复制/粘贴到 R...
    • 是的,不幸的是,使用 ggplotly 从 ggplot 创建的交互式 plotly 图表的行为不符合预期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2018-10-25
    相关资源
    最近更新 更多