Plotly

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

Mixed Subplot

Mixed Subplot

# 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)
p

Subplots

Subplots

Basic Subplots

La función subplot() proporciona una interfaz flexible para fusionar objetos en un solo gráfico.

Scaled Subplots

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.

Recursive Subplots

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).

3D Subplots

3D Subplot

# 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'))
p

Multiple Axes

Multiple Y Axes

Inset Plots

Basic Inset

Map Subplots

Basic Subplots with Maps

Referencias

Plotly 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/