Skip to content

Instantly share code, notes, and snippets.

@bchartoff
Created December 10, 2013 22:21
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/7901368 to your computer and use it in GitHub Desktop.
Save bchartoff/7901368 to your computer and use it in GitHub Desktop.
Code to create multi axis plots 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
)
),
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
)
),
yaxis2 = 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
),
autorange = FALSE,
range = c(0,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 = "probability",
side = "right"
),
xaxis = list(
title = "Time between eruptions (minutes)"
),
yaxis2 = list(
anchor = "x",
side = "left",
overlaying = "y",
title = "Length of eruption (minutes)",
autorange = FALSE,
range = c(0,6)
)
)
traceHistogram <- list(
y = faithful['waiting'][,1],
type = 'histogramy',
histnorm = 'probability',
autobinx = FALSE,
marker = list(
color = "#00000"
),
xbins = list(
start = 40,
end = 98,
size = 2
)
)
traceScatter <- list(
x = faithful['waiting'][,1],
y = faithful['eruptions'][,1],
type = 'scatter',
mode = 'markers',
marker = list(
color = '#00BFC4'
),
yaxis = 'y2'
)
response <- py$plotly(traceHistogram,traceScatter, 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