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

具有多个系列的绘图

  •  0
  • msh855  · 技术社区  · 7 年前

    我想复制下面的图表。我已经到了可以使用plot\ly在R中复制条形图的阶段。但是,我没有在次轴中添加线段。那么,有人能帮我在图表代码中添加什么,以获得类似的轴吗。我有下面的详细信息和MWE。

    enter image description here

    我正在尝试使用plot\ly。这是我的数据框架和我所做的:

    # download data  
    if (!file.exists("SCFP2013.xlsx")) {
      download.file("https://www.federalreserve.gov/econres/files/scfp2013excel.zip", "SCFP2013.zip")
      unzip("scfp2013.zip")
    } 
    
    df <- read_excel("SCFP2013.xlsx")
    df[df$NETWORTH < 0, ]$NETWORTH <- 0 # if net worth is less than 0, assign a value of zero
    
    # compute values of retirmet assets 
    households2 <- data.frame(
      netWorth = df$NETWORTH,
      weight = df$WGT,
      RETQLIQ = df$RETQLIQ    
    )
    
    # group households into segments
    households2 <- households2[households2$netWorth >= 10000, ]
    
    # split into net worth segments 
    nw2 <- floor(log10(households2$netWorth))
    segment2 <- ifelse(nw2 == 4, "     $10k", 
                       ifelse(nw2 == 5, "    $100K", 
                              ifelse(nw2 == 6, "   $1M",               
                                     ifelse(nw2 == 7, "  $10M",               
                                            ifelse(nw2 == 8, " $100M",               
                                                   "$1B+"))))) 
    
    # compute average asset distrubtions
    results2            <- as.data.frame((aggregate(households2,list(segment2),mean)))
    results2$life.cycle <- results2$RETQLIQ/results2$netWorth
    
    plot_ly(results2, x = ~Group.1, y = ~RETQLIQ, type = 'bar', name = 'Retirement (Pension/IRA)') %>%
      layout(yaxis = list(title = 'Millions'), xaxis = list(title = "Net Worth"),
             title = "Pensions Wealth", barmode = 'stack')
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Martin C. Arnold    7 年前

    您可以通过使用添加另一个跟踪来实现这一点 type='scatter' mode='lines' (我给它起了个名字 life.cycle )并在 layout(...) :

    plot_ly(results2, x = ~Group.1, y = ~RETQLIQ, 
           type = 'bar', name = 'Retirement (Pension/IRA)') %>%
    
    add_trace(x = ~Group.1, y = ~life.cycle, 
              type = 'scatter', mode = 'lines', name = 'life.cycle', 
              yaxis = 'y2', line = list(color = 'orange')) %>%
    
      layout(title = "Pensions Wealth", barmode = 'stack',
             xaxis = list(title = "Net Worth"),
             yaxis = list(side="left", title = 'Millions'),
             yaxis2 = list(side = 'right', overlaying = "y", title = '',
             showgrid = FALSE, zeroline = FALSE))  
    

    enter image description here

        2
  •  0
  •   Divi    7 年前

    您只需在当前绘图上添加一条额外的线。下面这样的方法会奏效。 ay 提供布局详细信息,您可以根据需要进行修改。

    ay <- list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = ""
    )
    
    plot_ly(results2, x = ~Group.1, y = ~RETQLIQ, type = 'bar', name = 'Retirement (Pension/IRA)') %>%
      add_lines(x = ~Group.1, y = ~life.cycle, name = "Life Cycle", yaxis = "y2") %>%
      layout(yaxis = list(title = 'Millions'), xaxis = list(title = "Net Worth"), yaxis2 = ay,
             title = "Pensions Wealth", barmode = 'stack')