CSV or comma-delimited-values is a very popular format for storing structured data. In this tutorial, we will see how to plot beautiful graphs using csv data. We will learn how to import csv data from an external source (a URL), and plot it using Plotly.
First we import the data and look at it.
data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv') head(data)
## AAPL_x AAPL_y ## 1 2014-01-02 77.44539 ## 2 2014-01-03 77.04558 ## 3 2014-01-06 74.89697 ## 4 2014-01-07 75.85646 ## 5 2014-01-08 75.09195 ## 6 2014-01-09 76.20263
Plot from CSV with Plotly
library(plotly) data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv') fig <- plot_ly(x = as.Date(data$AAPL_x), y = data$AAPL_y, type = 'scatter', mode = 'lines' , name = 'Share Prices (in USD)')%>% layout(title = 'Apple Share Prices over time (2014)', plot_bgcolor='#e5ecf6', xaxis = list( title = 'AAPL_x', zerolinecolor = '#ffff', zerolinewidth = 2, gridcolor = 'ffff'), yaxis = list( title = 'AAPL_y', zerolinecolor = '#ffff', zerolinewidth = 2, gridcolor = 'ffff'), showlegend = TRUE, width = 1100) fig
See https://plotly.com/r/getting-started for more information about Plotly's R API!
What About Dash?
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see
fig, you can display the same figure in a Dash for R application by passing it to the
figure argument of the
Graph component from the built-in
dashCoreComponents package like this:
library(plotly) fig <- plot_ly() # fig <- fig %>% add_trace( ... ) # fig <- fig %>% layout( ... ) library(dash) library(dashCoreComponents) library(dashHtmlComponents) app <- Dash$new() app$layout( htmlDiv( list( dccGraph(figure=fig) ) ) ) app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)