# Get this figure: fig <- get_figure("mmester", 101) # Get this figure's data: data <- get_figure("mmester", 101)$data # Add data to this figure: p <- add_trace(p, x=c(4, 5), y=c(4, 5), kwargs=list(filename="U.S. alcohol expenditure", fileopt="extend")) # Get y data of first trace: y1 <- get_figure("mmester", 101)$data[[1]]$y # Get figure documentation: https://plotly.com/r/get-requests/ # Add data documentation: https://plotly.com/r/file-options/ # You can reproduce this figure in R with the following code! # Learn about API authentication here: https://plotly.com/r/getting-started # Find your api_key here: https://plotly.com/settings/api library(plotly) trace1 <- list( uid = "a523ba", name = "Alcohol expenditure (% of after-tax income)", type = "scatter", xsrc = "mmester:98:a97151", x = c("1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"), ysrc = "mmester:98:3c5efc", y = c("1.29%", "1.34%", "1.17%", "1.16%", "1.03%", "1.00%", "1.01%", "0.97%", "0.98%", "0.84%", "0.84%", "0.82%", "0.89%", "0.84%", "0.81%", "0.78%", "0.90%", "0.78%", "0.80%", "0.80%", "0.88%", "0.76%", "0.86%", "0.75%", "0.72%", "0.72%", "0.68%", "0.74%", "0.71%", "0.79%", "0.79%") ) trace2 <- list( uid = "f0ca73", fill = "tozeroy", line = list( shape = "hvh", width = 0 ), mode = "lines", name = "Recessions", type = "scatter", xsrc = "mmester:98:7fda7c", x = c("1984", "1985", "1986", "1987", "1988", "1989", "1990.583", "1991.25", "1991.3", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001.25", "2001.92", "2002", "2004", "2005", "2006", "2007.95", "2008", "2009.5", "2009.55", "2011", "2012", "2013", "2014"), ysrc = "mmester:98:e6a036", y = c("0%", "0%", "0%", "0%", "0%", "0%", "1.5%", "1.5%", "0%", "0%", "0%", "0%", "0%", "0%", "0%", "0%", "0%", "1.5%", "1.5%", "0%", "0%", "0%", "0%", "1.5%", "1.5%", "1.5%", "0%", "0%", "0%", "0%", "0%"), visible = TRUE, fillcolor = "rgba(204, 204, 204, 0.5)", hoverinfo = "none", showlegend = FALSE ) data <- list(trace1, trace2) layout <- list( title = "U.S. consumer alcohol spending has
gradually declined since mid-1980s", width = 800, xaxis = list( type = "linear", range = c(1984, 2014), title = "", autorange = TRUE, tickformat = "", ticksuffix = "" ), yaxis = list( range = c(0, 1.5789473684210527), title = "Household alcohol spend (% of after-tax income)", autorange = TRUE, tickformat = "", ticksuffix = "%", exponentformat = "B" ), height = 560, autosize = FALSE, showlegend = FALSE, annotations = list( list( x = 0, y = -0.2, font = list(color = "rgb(102, 102, 102)"), text = "Note: Shaded ares denote recessions
Source: BLS CES, NBER", xref = "paper", yref = "paper", align = "left", xanchor = "left", yanchor = "bottom", showarrow = FALSE ) ), hidesources = TRUE ) p <- plot_ly() p <- add_trace(p, uid=trace1$uid, name=trace1$name, type=trace1$type, xsrc=trace1$xsrc, x=trace1$x, ysrc=trace1$ysrc, y=trace1$y) p <- add_trace(p, uid=trace2$uid, fill=trace2$fill, line=trace2$line, mode=trace2$mode, name=trace2$name, type=trace2$type, xsrc=trace2$xsrc, x=trace2$x, ysrc=trace2$ysrc, y=trace2$y, visible=trace2$visible, fillcolor=trace2$fillcolor, hoverinfo=trace2$hoverinfo, showlegend=trace2$showlegend) p <- layout(p, title=layout$title, width=layout$width, xaxis=layout$xaxis, yaxis=layout$yaxis, height=layout$height, autosize=layout$autosize, showlegend=layout$showlegend, annotations=layout$annotations, hidesources=layout$hidesources)