Los ejemplos son tomados de la pƔgina oficial de plotly, principalmente se obtienen algunos ejemplos grƔficos de plotly.
LibrerĆas
require(plotly)
require(htmlwidgets)
require(knitr)
require(tidyr)
require(dplyr)
require(rjson)
require(igraph)
require(igraphdata)
require(jpeg)
require(EBImage)
require(geomorph)
require(scales)
require(anglr)
require(maptools)data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/polar_dataset.csv")
p <- plot_ly(data,
type = 'scatterpolar',
mode = 'lines') %>%
add_trace(r = ~x1,
theta = ~y,
name = 'Figure8',
line = list(color = 'peru')) %>%
add_trace(r = ~x2,
theta = ~y,
name = 'Cardioid',
line = list(color = 'darkviolet')) %>%
add_trace(r = ~x3,
theta = ~y,
name = 'Hypercardioid',
line = list(color = 'deepskyblue')) %>%
add_trace(r = ~x4,
theta = ~y,
name = 'Subcardioid',
line = list(color = 'orangered')) %>%
add_trace(r = ~x5,
theta = ~y,
name = 'Supercardioid',
line = list(color = 'green')) %>%
layout(title = 'Mic Patterns',
font = list(family = 'Arial', size = 12, color = '#000'),
showlegend = F)
pp <- plot_ly(type = 'scatterpolar',
mode = 'lines') %>%
add_trace(r = c(0, 1.5, 1.5, 0, 2.5, 2.5, 0),
theta = c(0, 10, 25, 0, 205, 215, 0),
fill = 'toself',
fillcolor = '#709Bff',
line = list(color = 'black')) %>%
add_trace(r = c(0, 3.5, 3.5, 0),
theta = c(0, 55, 75, 0),
fill = 'toself',
fillcolor = '#E4FF87',
line = list(color = 'black')) %>%
add_trace(r = c(0, 4.5, 4.5, 0, 4.5, 4.5, 0),
theta = c(0, 100, 120, 0, 305, 320, 0),
fill = 'toself',
fillcolor = '#FFAA70',
line = list(color = 'black')) %>%
add_trace(r = c(0, 4, 4, 0),
theta = c(0, 165, 195, 0),
fill = 'toself',
fillcolor = '#FFDF70',
line = list(color = 'black')) %>%
add_trace(r = c(0, 3, 3, 0),
theta = c(0, 262.5, 277.5, 0),
fill = 'toself',
fillcolor = '#B6FFB4',
line = list(color = 'black')) %>%
layout(polar = list(radialaxis = list(visible = T, range = c(0,5))),
showlegend = F)
pp <- plot_ly(type = 'scatterpolar',
mode = 'lines') %>%
add_trace(r = c(5, 4, 2, 4, 5),
theta = c("a", "b", "c", "d", "a"),
name = 'angular categories',
fill = 'toself') %>%
add_trace(r = c("a", "b", "c", "d", "b", "f", "a"),
theta = c(1, 4, 2, 1.5, 1.5, 6, 5),
thetaunit = 'radians',
name = 'radial categories',
fill = 'toself',
subplot = 'polar2') %>%
add_trace(r = c(5, 4, 2, 4, 5),
theta = c("a", "b", "c", "d", "a"),
name = 'angular categories (w/ categoryarray)',
fill = 'toself',
subplot = 'polar3') %>%
add_trace(r = c("a", "b", "c", "d", "b", "f", "a", "a"),
theta = c(45, 90, 180, 200, 300, 15, 20, 45),
name = 'radial categories (w/ category descending)',
fill = 'toself',
subplot = 'polar4') %>%
layout(polar = list(domain = list(x = c(0,0.46),
y = c(0.56,1)),
radialaxis = list(angle = 45),
angularaxis = list(direction = 'clockwise', period = 6)),
polar2 = list(domain = list(x = c(0,0.46),
y = c(0,0.44)),
radialaxis = list(angle = 180,
tickangle = -180)),
polar3 = list(domain = list(x = c(0.54,1),
y = c(0.56,1)),
sector = c(150,400),
radialaxis = list(angle = -45),
angularaxis = list(categoryarray = c("d", "a", "c", "b"))),
polar4 = list(domain = list(x = c(0.54,1),
y = c(0,0.44)),
radialaxis = list(categoryorder = "category descending"),
angularaxis = list(thetaunit= "radians",
dtick = 0.3141592653589793)))
pp <- plot_ly(type = 'scatterpolar',
mode = "lines+markers") %>%
add_trace(r = c(1,2,3,4,5),
theta = c(0,90,180,360,0),
line = list(color = "#ff66ab"),
marker = list(color = "#8090c7",
symbol = 'square',
size = 8),
text = "sector: 135->225<br>rotation: 90<br>direction: counterclockwise") %>%
add_trace(r = c(1,2,3,4,5),
theta = c(0,90,180,360,0),
line = list(color = "#ff66ab"),
marker = list(color = "#8090c7",
symbol = 'square',
size = 8),
text = "sector: 135->225<br>rotation: 90<br>direction: counterclockwise",
subplot = 'polar2') %>%
layout(polar = list(domain = list(x = c(0,0.4),
y = c(0,1)),
radialaxis = list(tickfont = list(size = 8)),
angularaxis = list(tickfont = list(size = 8),
rotation = 90,
direction = 'counterclockwise')),
polar2 = list(domain = list(x = c(0.6,1),
y = c(0,1)),
radialaxis = list(tickfont = list(size = 8)),
angularaxis = list(tickfont = list(size = 8),
rotation = 90,
direction = 'clockwise')),
showlegend = F)
pp <- plot_ly(type = 'scatterpolar',
mode = "lines+markers") %>%
add_trace(r = c(1,2,3,4,5),
theta = c(0,90,180,360,0),
line = list(color = "#ff66ab"),
marker = list(color = "#8090c7",
symbol = 'square',
size = 8)) %>%
add_trace(r = c(1,2,3,4,5),
theta = c(0,90,180,360,0),
line = list(color = "#ff66ab"),
marker = list(color = "#8090c7",
symbol = 'square',
size = 8),
subplot = 'polar2') %>%
layout(polar = list(domain = list(x = c(0,0.4),
y = c(0,1)),
sector = c(150,210),
radialaxis = list(
tickfont = list(size = 8)),
angularaxis = list(tickfont = list(size = 8))),
polar2 = list(domain = list(x = c(0.6,1),
y = c(0,1)),
radialaxis = list(tickfont = list(size = 8)),
angularaxis = list(tickfont = list(size = 8))),
showlegend = F)
pp <- plot_ly(type = 'scatterpolar',
mode = 'lines') %>%
add_trace(r = c(1,2,3),
theta = c(50,100,200),
marker = list(symbol = 'square')) %>%
add_trace(r = c(1,2,3),
theta = c(1,2,3),
thetaunit = 'radians') %>%
add_trace(r = c("a", "b", "c", "d"),
theta = c("D","C","B","A"),
subplot = 'polar2') %>%
add_trace(r = c(50,300,900),
theta = c(0,90,180),
subplot = 'polar3') %>%
add_trace(r = c(3,3,4,3),
theta = c(0,45,90,270),
fill = 'toself',
subplot = 'polar4') %>%
layout(polar = list(domain = list(x = c(0,0.46),
y = c(0.56,1)),
radialaxis = list(range = c(1,4)),
angularaxis = list(thetaunit = 'radians')),
polar2 = list(domain = list(x = c(0,0.46),
y = c(0,0.42))),
polar3 = list(domain = list(x = c(0.54,1),
y = c(0.56,1)),
sector = c(0,180),
radialaxis = list(type = 'log', angle = 45)),
polar4 = list(domain = list(x = c(0.54,1),
y = c(0,0.44)),
radialaxis = list(visible = F,
range = c(0,6))),
showlegend = F)
pdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/hobbs-pearson-trials.csv")
j = 1
k = 2
p <- plot_ly(type = 'scatterpolargl',
mode = 'markers')
for (i in 1:(length(data)/2)){
p <- p %>%
add_trace(r = data[,j],
theta = data[,k],
name = paste('Trial ', i),
marker = list(size = 15,
line = list(color = '#FFF'),
opacity = 0.7)) %>%
layout(title = "Hobbs-Pearson Trials",
showlegend = F,
paper_bgcolor = "rgb(223, 223, 223)",
polar = list(bgcolor = "rgb(223, 223, 223)",
angularaxis = list(tickwidth = 2,
linewidth = 3,
layer = 'below traces'),
radialaxis = list(side = 'counterclockwise',
showline = T,
linewidth = 2,
tickwidth = 2,
gridcolor = '#FFF',
gridwidth = 2)))
j <- j + 2
k <- k + 2
}
pp <- plot_ly(type = 'scatterpolar',
fill = 'toself') %>%
add_trace(r = c(39, 28, 8, 7, 28, 39),
theta = c('A','B','C', 'D', 'E', 'A'),
name = 'Group A') %>%
add_trace(r = c(1.5, 10, 39, 31, 15, 1.5),
theta = c('A','B','C', 'D', 'E', 'A'),
name = 'Group B') %>%
layout(polar = list(radialaxis = list(visible = T,
range = c(0,50))))
prequire(jpeg)
# Image processing
url <- "https://images.plot.ly/plotly-documentation/images/heatmap-galaxy.jpg"
tmpf <- tempfile()
download.file(url,tmpf,mode="wb")
data <- readJPEG(tmpf)
fr <- file.remove(tmpf) # remove the downloaded temp file
zdata = rowSums(data*255, dims = 2)
p <- plot_ly(z = zdata,
colorscale = list(c(0,0.5,1),c("blue", "white", "red")),
type = "heatmapgl")
pEl Image Plot solo acepta imƔgenes multicanal. Para imƔgenes individuales, se usa heatmap. El plot de la imagen es diferente de la clase layout, que se puede usar para agregar imƔgenes de fondo o logotipos a las figuras.
Para crear una matriz numĆ©rica que se pasarĆ” a una Plot Image, se puede utilizar una librerĆa como EBImage para abrir una imagen desde una URL.
El rango de datos y el rango de color se mapean juntos usando los parƔmetros zmin y zmax, que corresponden respectivamente a los valores de datos mapeados en black[0, 0, 0] y white[255, 255, 255]. El valor predeterminado de zmin y zmax depende del colormodel. En este ejemplo colormodel es rgb(de forma predeterminada), el valor predeterminado de zmin is [0, 0, 0] y zmax is [250, 250, 250].
Puede crear formas de diseño mediante programación, pero también puede dibujar formas manualmente configurando el dragmode en uno de los modos de dibujo de formas: drawline,drawopenpath, drawclosedpath, drawcircle, o drawrect. Si necesita cambiar entre diferentes dibujos de formas u otros dragmode (panning, selecting, etc.), se pueden agregar botones de la barra de modo en la configuración de su figura para seleccionar el modo de arrastre.
img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
p <- plot_ly(type="image",
z=img*255) %>%
layout(dragmode="drawrect",
xaxis = list(title = 'Click and drag inside the figure to draw a rectangle or select another shape in the modebar')) %>%
config(modeBarButtonsToAdd = list("drawline",
"drawopenpath",
"drawclosedpath",
"drawcircle",
"drawrect",
"eraseshape"
))
pp <- plot_ly(x = c(1, 2, 3),
y = c(1, 2, 3)) %>%
add_lines() %>%
layout(images = list(list(source = "https://images.plot.ly/language-icons/api-home/python-logo.png",
xref = "paper",
yref = "paper",
x= 0,
y= 1,
sizex = 0.2,
sizey = 0.2,
opacity = 0.8),
list(source = "https://images.plot.ly/language-icons/api-home/matlab-logo.png",
xref = "x",
yref = "paper",
x = 2.7,
y = 0.25,
sizex = 0.4,
sizey = 0.8,
opacity = 0.8),
list(source = "https://images.plot.ly/language-icons/api-home/r-logo.png",
xref = "x",
yref = "y",
x = 0.9,
y = 3.1,
sizex = 2,
sizey = 2,
sizing = "stretch",
opacity = 0.4,
layer = "below")))
pPlotly R Graphing Library | R | Plotly. (2020). Retrieved August 5, 2020, from https://plotly.com/r/