This is a quick and brief sample of my final project graph using plotly to chart the average CPI value through the history of the CPI index:
#install.packages('plotly')
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
CPI <- read.csv('CPI.csv')
head(CPI)
## year mean
## 1 1947 2104.083
## 2 1948 2293.500
## 3 1949 2232.333
## 4 1950 2276.667
## 5 1951 2474.167
## 6 1952 2552.083
p <- plot_ly(data=CPI, x= ~year, y= ~mean)
p
## 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
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning in arrange_impl(.data, dots): '.Random.seed' is not an integer
## vector but of type 'NULL', so ignored
The Google charting tool googleVis looks incredibly useful, as it automates the process for embedding a chart into HTML. Unfortunately with the data I’ve chosen the gvisMotionChart doesn’t work (requires 4 columns, I only have 2) but below I’ve outlined the steps regardless.
#install.packages('googleVis')
library(googleVis)
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
##
## Welcome to googleVis version 0.6.2
##
## Please read Google's Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
##
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
##
## See the googleVis package vignettes for more details,
## or visit http://github.com/mages/googleVis.
##
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
The M$html prompt will return the HTML code, which for our purposes is a bit unsightly.
M <- gvisMotionChart(CPI, idvar='mean', timevar='year', options=list())
M$type
## [1] "MotionChart"
M$chartid
## [1] "MotionChartID25643a8956a9"
#M$html
Using the gvisLineChart however is possible but does not display in RMarkdown:
plot(gvisLineChart(CPI, xvar = 'year', yvar = 'mean'))
## starting httpd help server ...
## done