0

I want to replicate this graph below. I have reached a stage where I could reproduce the bar chart in R, using plot_ly. But, I rather failed to add the line segment in the secondary axis. So, can someone help me what I should add in my chart code, to get a similar axis. I have the details and a MWE below.

enter image description here

I am trying to use plot_ly. Here is my dataframe and what I did:

# 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 Answers 2

1

You can achieve this by adding another trace with type='scatter' and mode='lines' (I named it life.cycle) and setting up an additional y-axis in 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

Sign up to request clarification or add additional context in comments.

Comments

0

You simply need to add an additional line over your current plot. Something like below will work. ay provides the layout details which you can modify based on your requirement.

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.