Mixed Subplots in R
How to create mixed subplots in R with Plotly.
New to Plotly?
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Mixed Subplot
library(plotly)
# read in Walmart data
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv")
# first plot - bar chart
total <- plyr::count(df$YEAR)
fit <- fitted(loess(total$freq ~ total$x))
fig2 <- plot_ly(data = total, x = ~x, y = ~freq, type = "bar", showlegend=FALSE,
marker=list(color=~x, showscale=FALSE))
fig2 <- fig2 %>% add_lines(y = fit, showlegend=FALSE, color = 'black')
fig2 <- fig2 %>% layout(showlegend=FALSE, xaxis = list(side="right", showgrid=FALSE),
yaxis=list(showgrid=FALSE))
# second plot - scattergeo map
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white'))
fig3 <- plot_geo(df, lat = ~LAT, lon = ~LON)
fig3 <- fig3 %>% add_markers(
text = ~OPENDATE, showlegend=FALSE,
marker=list(color = ~YEAR, showscale=FALSE),
hoverinfo = "text")
fig3 <- fig3 %>% layout(geo = g, showlegend=FALSE)
# third plot - 3D mesh
#devtools::install_github("hypertidy/anglr")
library(anglr)
library(maptools)
data(wrld_simpl)
map1 <- subset(wrld_simpl,
NAME %in% c("Indonesia", "Papua New Guinea", "New Zealand", "Australia"))
## DEL model (like TRI in silicate)
delmesh <- anglr::globe(anglr::DEL(map1, max_area = 0.5))
mesh <- as.mesh3d(delmesh)
# plot point cloud
x <- mesh$vb[1,]
y <- mesh$vb[2, ]
z <- mesh$vb[3,]
m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))
# colours in z don't make sense here, need to mafig object aesthetics above
zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})
library(scales)
facecolor = colour_ramp(
brewer_pal(palette="RdBu")(9)
)(rescale(x=zmean))
fig1 <- plot_ly(
x = x, y = y, z = z,
i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
facecolor = facecolor,
type = "mesh3d"
)
# subplot
fig <- subplot(fig1, fig2, fig3, nrows = 2)
fig <- fig %>% layout(title = "Walmart Store Openings by Year",
xaxis = list(domain=list(x=c(0,0.5),y=c(0,0.5))),
scene = list(domain=list(x=c(0.5,1),y=c(0,0.5))),
xaxis2 = list(domain=list(x=c(0.5,1),y=c(0.5,1))),
showlegend=FALSE,showlegend2=FALSE)
fig
Reference
See https://plotly.com/r/reference for more information and options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)