Los ejemplos son tomados de la página oficial de plotly, principalmente se obtienen algunos ejemplos gráficos con la estructura básica de plotly.
LibrerÃas
# 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))
p2 <- plot_ly(data = total,
x = ~x,
y = ~freq,
type = "bar",
showlegend=FALSE,
marker=list(color=~x, showscale=FALSE)) %>%
add_lines(y = fit,
showlegend=FALSE,
color = 'black') %>%
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'))
p3 <- plot_geo(df,
lat = ~LAT,
lon = ~LON) %>%
add_markers(text = ~OPENDATE,
showlegend=FALSE,
marker=list(color = ~YEAR, showscale=FALSE),
hoverinfo = "text") %>%
layout(geo = list(scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')),
showlegend=FALSE)
# third plot - 3D mesh
#devtools::install_github("hypertidy/anglr")
require(anglr)
require(maptools)
require(scales)
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])})
p1 <- plot_ly(x = x,
y = y,
z = z,
i = mesh$it[1,]-1,
j = mesh$it[2,]-1,
k = mesh$it[3,]-1,
facecolor = colour_ramp(brewer_pal(palette="RdBu")(9))(rescale(x=zmean)),
type = "mesh3d")
# subplot
p <- subplot(p1, p2, p3, nrows = 2) %>%
layout(title = "Walmart Store Openings by Year",
xaxis = list(domain = list(x = c(0,0.5),y = c(0,0.5))),
xaxis2 = list(domain = list(x = c(0.5,1),y = c(0.5,1))),
scene = list(domain = list(x = c(0.5,1),y = c(0,0.5))),
showlegend = FALSE, showlegend2 = FALSE)
pLa función subplot() proporciona una interfaz flexible para fusionar objetos en un solo gráfico.
Aunque la función subplot() acepta un número arbitrario de gráficos, pasar a una lista de gráficos puede ahorrar escritura y código redundante cuando se trata de un gran número de gráficos. Por ejemplo, puede compartir el eje x utilizando shareX, establecer el ID del eje y especificar el número de filas con nrows.
La función subplot() devuelve un objeto gráfico para que se pueda modificar como cualquier otro gráfico. Significa que los subtramas funcionan de forma recursiva (es decir, puede tener gráficos dentro de otros gráficos).
plotList <- function(nplots) {
lapply(seq_len(nplots), function(x)
plot_ly(mtcars,
x = ~wt,
y = ~mpg,
type = "scatter",
color = ~factor(cyl),
size = ~ cyl,
colors = "Set2"))
}
s1 <- subplot(plotList(6),
nrows = 2,
shareX = TRUE,
shareY = TRUE)
s2 <- subplot(plotList(2),
shareY = TRUE)
p <- subplot(s1, s2,
plot_ly(),
nrows = 3,
margin = 0.04,
heights = c(0.6, 0.3, 0.1))
p# individual plots
p1 <- plot_ly(z = ~volcano,
scene='scene1') %>%
add_surface(showscale=FALSE)
p2 <- plot_ly(z = ~volcano,
scene='scene2') %>%
add_surface(showscale=FALSE)
p3 <- plot_ly(z = ~volcano,
scene='scene3') %>%
add_surface(showscale=FALSE)
p4 <- plot_ly(z = ~volcano,
scene='scene4') %>%
add_surface(showscale=FALSE)
# subplot and define scene
p <- subplot(p1, p2, p3, p4) %>%
layout(title = "3D Subplots",
scene = list(domain = list(x = c(0,0.5),y = c(0.5,1)),
xaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
aspectmode = 'cube'),
scene2 = list(domain = list(x = c(0.5,1),y = c(0.5,1)),
xaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
aspectmode = 'cube'),
scene3 = list(domain = list(x = c(0,0.5),y = c(0,0.5)),
xaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
aspectmode = 'cube'),
scene4 = list(domain = list(x = c(0.5,1),y = c(0,0.5)),
xaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
aspectmode = 'cube'))
pp <- plot_ly() %>%
add_lines(x = ~1:3,
y = ~10*(1:3),
name = "slope of 10") %>%
add_lines(x = ~2:4,
y = ~1:3,
name = "slope of 1",
yaxis = "y2") %>%
layout(title = "Double Y Axis",
yaxis2 = list(tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"),
xaxis = list(title="x"))
pdf <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv')
one_map <- function(dat) {
plot_geo(dat) %>%
add_markers(x = ~LON,
y = ~LAT,
color = I("blue"),
alpha = 0.5) %>%
add_text(x = -78,
y = 47,
text = ~unique(YEAR),
color = I("black")) %>%
layout(geo = list(scope = 'usa', # common map properties
showland = T,
landcolor = toRGB("gray90"),
showcountries = F,
subunitcolor = toRGB("white")))
}
data <- df %>%
group_by(YEAR) %>%
do(mafig = one_map(.))
p <- data %>%
subplot(nrows = 9) %>%
layout(showlegend = FALSE,
title = 'New Walmart Stores per year 1962-2006<br> Source: <a href="http://www.econ.umn.edu/~holmes/data/WalMart/index.html">University of Minnesota</a>',
width = 1000,
height = 900,
hovermode = FALSE)
pPlotly R Graphing Library | R | Plotly. (2020). Retrieved August 5, 2020, from https://plotly.com/r/
Interactive web-based data visualization with R, plotly, and shiny. (2019). Interactive Web-Based Data Visualization with R, Plotly, and Shiny. Retrieved August 16, 2020, from https://plotly-r.com/