Skip to content

Instantly share code, notes, and snippets.

@bchartoff
Created December 11, 2013 01:54
Show Gist options
  • Save bchartoff/7903916 to your computer and use it in GitHub Desktop.
Save bchartoff/7903916 to your computer and use it in GitHub Desktop.
Old faithful data with histograms on both axes in plotly
#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
)
)
}
}
##########################################################
legend <- FALSE
gglayout <- list(
showlegend = legend,
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
),
xaxis2 = 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
)
),
yaxis3 = list(
titlefont = list(
color = "#7f7f7f",
face = "Arial, sans-serif"
),
gridcolor = "#00BFC4",
zeroline = FALSE,
showline = FALSE,
mirror = FALSE,
tickwidth =1,
ticklen = 5,
tickcolor = "00BFC4",
tickfont = list(
color = "#00BFC4",
face = "Arial, sans-serif",
size = 9.6
),
legend = list(
bgcolor = 'F2F2F2',
bordercolor = 'rgba(0,0,0,0)',
x = 1.4
),
width = 554,
height = 539,
margin = margins(legend)
)
library(plotly)
py <- plotly(username='bchartoff', key='guww2lu2dd')
layout <- list(
title = "Old Faithful",
yaxis = list(
title = "Length of eruptions (minutes)",
domain = c(0,.8),
autorange = FALSE,
range = c(1.5,5.5),
mirror = FALSE
),
xaxis = list(
title = "Time between eruptions (minutes)",
domain = c(0,.8),
mirror = FALSE
),
xaxis2= list(
domain = c(.8,1),
showgrid = FALSE,
showline = FALSE,
zeroline = FALSE,
mirror = FALSE
),
yaxis3 = list(
domain = c(.8,1),
showgrid = FALSE,
showline = FALSE,
zeroline = FALSE,
mirror = FALSE
)
)
traceHistogramY <- list(
y = faithful['waiting'][,1],
type = 'histogramy',
histnorm = 'probability',
autobinx = FALSE,
marker = list(
color = "#00000"
),
xaxis = 'x',
yaxis = 'y3',
xbins = list(
start = 40,
end = 98,
size = 2
)
)
traceHistogramX <- list(
x = faithful['eruptions'][,1],
type = 'histogramx',
histnorm = 'probability',
autobinx = FALSE,
marker = list(
color = "#00000"
),
xaxis = 'x2',
yaxis = 'y',
bardir = 'h',
xbins = list(
start = 0,
end = 6,
size = .1786
)
)
traceScatter <- list(
x = faithful['waiting'][,1],
y = faithful['eruptions'][,1],
type = 'scatter',
mode = 'markers',
marker = list(
color = '#00BFC4'
)
)
response <- py$plotly(list(traceScatter, traceHistogramY, traceHistogramX ), kwargs = list(layout = appendList(gglayout,layout)))
# url and filename
url <- response$url
filename <- response$filename
browseURL(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment