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

