Introduction

Let’s draw a graph showing the increase in personal consumption expenditures (pce) over time in the USA. In addition to expenditures, we’ll add information on the percentage of savings rate using a range of 4 levels. This dataset was produced from US economic time series data available from http://research.stlouisfed.org/fred2.

Loading data

data("economics")

economics$psavert <- cut2(economics$psavert, c(5,10,15))
levels(economics$psavert) <- c("personal savings rate <5%","5%<= personal savings rate <10%","10%<= personal savings rate <15%","personal savings rate>15%")

Drawing the graph

economics %>%
plot_ly(x=~date, 
        y =~pce, 
        mode="markers", 
        color = ~psavert)%>%
layout(legend = list(orientation = "h",   
                     xanchor = "center",  
                     x = 0.5,
                     y = -0.3))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter