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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
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
0 comments:
Post a Comment