Plotting BPI data with Plotly

plotly image 2

Plotly is a new tool I’ve recently come across for sharing interactive graphs. Its effectively an API that allows you to pump graphs to the web using various coding languages; currently R, Python and Matlab. It automatically adds an element of interactivity to the graphs using java script. Its then easy to share those graphs with other people. Its business model works in a similar way to github and bitbucket. It’s free if you are happy to share your output with the world but you have to pay for privacy. So for bloggers and open source enthusiasts its a useful free tool.

There are plenty of instructions on the site to guide you through using it, but here’s a quick overview of using it in R from me.

Firstly sign up to the plotly service. You will then get an ID and passcode. You will need these to push your graphs through the API.

Next set up plotly in R. Install the package from their github. Then apply your credentials. You will only need to put in your credentials once per R installation. So on your regular computer you do this once and then you can forget about it.

#install plotly library
install.packages("devtools")
library("devtools")
install_github("ropensci/plotly")
library(plotly)

#set credentials for api connection
set_credentials_file("userid", "code")

Next crack on and make your lovely graph in R. It works with basic charts and also some ggplot2 graphs. Here I’m using some data I took from the BPI annual report. I load in the libraries and my data then adjust the data format slightly to suit ggplot then make my graph.

library(ggplot2)
library(reshape2)
bpi_genre_sales_perc <- read.csv("http://myinspirationinformation.com/wp-content/uploads/2014/08/bpi_genre_sales_perc.csv") sales_perc_melt<-melt(bpi_genre_sales_perc) sales_perc_albums<-sales_perc_melt[sales_perc_melt$Type=="Albums",] colnames(sales_perc_albums)<-c("Type", "Genre", "Year", "Percent") albums<-ggplot(sales_perc_albums, aes(x=Year, y=Percent, group=Genre)) + geom_line(aes(color=Genre)) albums

Now I just need to send it over to plotly. Firstly I use the plotly() function to initialise the API with my credentials. I then access the API using a ggplotly method and push my graph through. I could just run API$ggplotly(albums) but the extra parts allow me to store my graph into a folder called "bpi_report" and name my chart "example" and finally to overwrite the graph if it exists already.

API <- plotly() p1 <- API$ggplotly(albums, kwargs=list(filename="bpi_report/example", fileopt="overwrite"))

And here is the output.

There are numerous options to share your graph -
By adding it to the object "p1" I can then extract the url from that object, put an iframe wrapper around it to pull the graph through in a markdown document.
You can extract the graph as a jpg or svg (though this slightly defeats the purpose)
You can post it to your social media account
Or as I have you can embed it in a blog.

I've made a few graphs from the BPI data below, the R script is in this gist.

Even though rock and pop dominate album sales, pop is the clear leader in single sales. You can also see the impact that digital has had on a physical album sales, and the increase in digital sales isn't making up for it.

Leave a Reply

Your email address will not be published. Required fields are marked *