Skip to content

Instantly share code, notes, and snippets.

@bchartoff
Created November 20, 2013 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bchartoff/7563949 to your computer and use it in GitHub Desktop.
Save bchartoff/7563949 to your computer and use it in GitHub Desktop.
####################################
# diamond data bar chart
# Questions? Email feedback@plot.ly
# For more docs, see plot.ly/api
####################################
library(plotly)
py <- plotly(username='USERNAME', key='API_KEY')
#return list of n ggplot default colors (evenly spaced around the color wheel)
gg_color_hue <- function(n) {
hues = seq(15, 375, length=n+1)
hcl(h=hues, l=65, c=100)[1:n]
}
#combine elements with same name in two named lists (useful for combining layouts)
appendList <- function (x, val)
{
stopifnot(is.list(x), is.list(val))
xnames <- names(x)
for (v in names(val)) {
x[[v]] <- if (v %in% xnames && is.list(x[[v]]) && is.list(val[[v]]))
appendList(x[[v]], val[[v]])
else c(x[[v]], val[[v]])
}
x
}
#returns margins, based on existence of legend
margins <- function(l){
if(l){
return (list(
b = 57,
t = 80,
r = 151,
l = 81,
pad = 0
)
)
}
else{
return (list(
b = 57,
t = 80,
r = 41,
l = 81,
pad = 0
)
)
}
}
##########################################################
library(ggplot2)
#convert data frame to list of lists used by plotly
l_diamonds <- lapply(split(diamonds,seq_along(diamonds[1,1])), function(l){list(as.list(l))})
#extract data
clarity <- get('clarity',l_diamonds[[1]][[1]])
cut <- row.names(table(get('cut',l_diamonds[[1]][[1]])))
y <- lapply(seq(5),function(i){
as.vector(
table(
get('clarity',subset(diamonds, cut==cut[i]))
)
)
}
)
#generate colors
ggcols <- gg_color_hue(5)
cols <- list(ggcols[1],ggcols[2],ggcols[3], ggcols[4],ggcols[5])
#populate dataset
#data <- as.list(seq())
data <-
lapply(seq(5),function(i){return(list(
x = row.names(table(clarity)),
y = y[i][[1]],
name = cut[i],
type = 'bar',
mode = 'stack',
marker = list(
color = rep(cols[i],length(row.names(table(clarity))))
)
))})
legend <- TRUE # change to TRUE to make legend visible and adjust layout (to ggplot standards)
#default layout options for ggplot default theme_grey exported to pdf through Quartz 2 (OSX)
gglayout <- list(
plot_bgcolor = "#E5E5E5",
font = list(
color = "#000000",
face = "Arial, sans-serif",
size = 12
),
xaxis = list(
gridcolor = "#FFFFFF",
gridwith = 1,
zeroline = FALSE,
showline = FALSE,
mirror = FALSE,
tickwidth =1,
ticklen = 5,
tickcolor = "7f7f7f",
tickfont = list(
color = "#7f7f7f",
face = "Arial, sans-serif",
size = 9.6
)
),
yaxis = list(
gridcolor = "#FFFFFF",
zeroline = FALSE,
showline = FALSE,
mirror = FALSE,
tickwidth =1,
ticklen = 5,
tickcolor = "7f7f7f",
tickfont = list(
color = "#7f7f7f",
face = "Arial, sans-serif",
size = 9.6
)
),
showlegend = legend,
legend = list(
bgcolor = 'F2F2F2',
bordercolor = 'rgba(0,0,0,0)',
x = 1.4
),
width = 554,
height = 539,
margin = margins(legend)
)
#layout options for this plot
plotlayout <- list(
xaxis = list(
title = 'wt'
),
yaxis = list(
title = 'mpg',
ticks = list(500,1000)
)
)
layout <- appendList(gglayout,plotlayout)
response<-py$plotly(data, kwargs = list(layout=layout))
# url and filename
url <- response$url
filename <- response$filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment