Thursday, April 07, 2016

Plot.ly on R

I just discovered this amazing graph package, Plot.ly. Who needs d3.js when plot.ly is so easy to use?!

Best part is, it works both online and offline. I can create a plot in R, and it renders it within my Rstudio with all the interactive functions. I can also upload my data to the plot.ly website and create a graph from there. Either ways allows me to save the graph into my plot.ly account.

To give it a test run, I used the shelter animals data from kaggle. (Reason: been trying to persuade my parents to adopt a shelter dog for the longest time)

library(plotly)
Sys.setenv("plotly_username"="melisasa")
Sys.setenv("plotly_api_key"="SECRET")
dataset = read.csv ("train.csv", header=TRUE)
dog = subset(dataset, AnimalType == "Dog")
for (i in 1:nrow(dog)) {
time = sub('.*\\ ', '', dog[i,"AgeuponOutcome"])
num_str = sub("\\ .*", "", dog[i,"AgeuponOutcome"])
num = strtoi(num_str, base = 0L)
if (time == "year" || time == "years")
dog[i,"Age_weeks"] = num*52
else (time == "month" || time == "months")
dog[i,"Age_weeks"] = num*4
dog[i,"Outcome_combined"] = paste(dog[i,"OutcomeType"], dog[i,"OutcomeSubtype"], sep="_")
}
age_outcome = plot_ly(dog, x=Age_weeks, color=OutcomeType, type="box")
age_outcome
plotly_POST(age_outcome, filename = "r-docs/Age_and_outcome_for_shelter_dogs")
age_outcome_deets = plot_ly(dog, x=Age_weeks, color=Outcome_combined, type="box")
age_outcome_deets
plotly_POST(age_outcome_deets, filename = "r-docs/Age_and_outcome_for_shelter_dogs")
So I extracted only shelter dog data and compared their age to the outcome of the shelter dog. I later broke down the outcomes to each subtype, and this is the outcome Honestly, I can't gather much insights from this, besides the fact that, the probability of a dog getting adopted after 20 weeks/5 months gets really low (below 75%). It also looks like most dogs don't survive beyond 40 weeks/8 months. They either pass on, or receive euthanasia before then. That's a really short time ):

Uploading github codes: http://robertgreiner.com/2012/04/using-github-as-a-syntax-highlighter/
Uploading plot.ly graphs from R: https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account

Related Articles

0 comments:

Post a Comment