Los ejemplos son tomados de la página oficial de plotly, principalmente se obtienen algunos ejemplos gráficos de plotly.
LibrerÃas
Si establece el atributo de hovermode(modo de desplazamiento) del diseño de su figura en x unificado o y unificado), aparecerá una sola etiqueta de desplazamiento, que describe un punto del plot, para puntos en el mismo valor x (o y) que el cursor.
data <- data.frame(trace_0 = rnorm(100, mean = 5),
trace_1 = rnorm(100, mean = 0),
trace_2 = rnorm(100, mean = -5),
x = c(1:100))
p <- plot_ly(data,
x = ~x,
y = ~trace_0,
name = 'trace 0',
type = 'scatter',
mode = 'lines') %>%
add_trace(y = ~trace_1,
name = 'trace 1',
mode = 'lines+markers') %>%
add_trace(y = ~trace_2,
name = 'trace 2',
mode = 'markers') %>%
layout(hovermode = "x unified")
pp <- plot_ly(type = 'scatter',
mode = 'markers') %>%
add_trace(x = c(1:100),
y = rnorm(100, mean = 5),
marker = list(color='green'),
hoverinfo = 'y',
showlegend = F) %>%
layout(title = list(text="Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>",
size = 10),
xaxis = list(zeroline = F),
yaxis = list(hoverformat = '.2f'))
pPara personalizar la información sobre herramientas en su gráfico, puede usar hovertemplate, que es una cadena de plantilla que se usa para representar la información que aparece en el cuadro flotante.
p <- plot_ly() %>%
add_trace(type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
'<br><b>X</b>: %{x}<br>',
'<b>%{text}</b>'),
showlegend = FALSE) %>%
add_trace(type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(3.02825,2.63728,4.83839,3.8485,1.73463),
hovertemplate = 'Price: %{y:$.2f}<extra></extra>',
showlegend = FALSE)
pdf <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/job-automation-probability.csv')
p <- df %>%
plot_ly(type = 'scatter',
mode = 'markers',
x = ~prob,
y = ~Average.annual.wage,
marker = list(size = ~numbEmployed, sizeref = 4000, sizemode = 'area'),
color = ~education,
text = ~short.occupation,
hovertemplate = paste("<b>%{text}</b><br><br>",
"%{yaxis.title.text}: %{y:$,.0f}<br>",
"%{xaxis.title.text}: %{x:.0%}<br>",
"Number Employed: %{marker.size:,}",
"<extra></extra>")) %>%
layout(legend = list(orientation = 'h', y = -0.3))
pdata <- data.frame(Primates = c('Potar monkey', 'Gorilla', 'Human', 'Rhesus monkey', 'Chimp'),
Bodywt = c(10.0, 207.0, 62.0, 6.8, 52.2),
Brainwt = c(115, 406, 1320, 179, 440))
p <- plot_ly(data,
x = ~Bodywt,
y = ~Brainwt,
type = 'scatter',
mode = 'text',
text = ~Primates,
textposition = 'middle right',
textfont = list(color = '#000000', size = 16)) %>%
layout(title = 'Primates Brain and Body Weight',
xaxis = list(title = 'Body Weight (kg)',
zeroline = TRUE,
range = c(0, 250)),
yaxis = list(title = 'Brain Weight (g)',
range = c(0,1400)))
pdata <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
p <- plot_ly(data,
x = ~wt,
y = ~mpg,
text = rownames(data)) %>%
add_markers() %>%
add_text(textfont = list(family = "sans serif", size = 14, color = toRGB("grey50")),
textposition = "top right") %>%
layout(xaxis = list(range = c(1.6, 3.2)),
showlegend = FALSE)
pdf <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv",
stringsAsFactors = FALSE) %>%
filter(year==2007 & continent=='Europe' & pop > 2.e6)
p <- plot_ly(df,
type='bar',
x = ~country,
y = ~pop,
text = ~lifeExp, name="",
hovertemplate = paste('%{x}', '<br>lifeExp: %{text:.2s}<br>'),
texttemplate = '%{y:.2s}', textposition = 'outside') %>%
layout(uniformtext=list(minsize=8, mode='hide'))
pdf <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv",
stringsAsFactors = FALSE) %>%
filter(year==2007 & continent=='Asia')
p <- plot_ly(df,
type='pie',
labels = ~country,
values = ~pop,
textposition = 'inside') %>%
layout(uniformtext=list(minsize=12, mode='hide'))
pdata <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
p <- plot_ly(data,
x = ~wt,
y = ~mpg,
type = 'scatter',
mode = 'markers',
marker = list(size = 10)) %>%
add_annotations(x = data$wt,
y = data$mpg,
text = rownames(data),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5,
ax = 20,
ay = -40)
pm <- economics[which.max(economics$unemploy), ]
n <- economics[which.max(economics$uempmed), ]
p1 <- plot_ly(economics,
x = ~date,
y = ~unemploy) %>%
add_lines(name = ~"unemploy") %>%
layout(annotations = list(x = m$date,
y = m$unemploy,
text = "annotation a",
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 7,
ax = 20,
ay = -40),
xaxis = list(title = "x Axis",
titlefont = list(family = "Courier New, monospace",
size = 18,
color = "#7f7f7f ")),
yaxis = list(title = "y Axis",
titlefont = list(family = "Courier New, monospace",
size = 18,
color = "#7f7f7f ")))
p2 <- plot_ly(economics,
x = ~date,
y = ~uempmed) %>%
add_lines(name = ~"uempmed") %>%
layout(annotations = list(x = n$date,
y = n$uempmed,
text = "annotation b",
xref = "x2",
yref = "y2",
showarrow = TRUE,
arrowhead = 7,
ax = 20,
ay = -40),
xaxis = list(title = "x Axis",
titlefont = list(family = "Courier New, monospace",
size = 18,
color = "#7f7f7f ")),
list(title = "y Axis",
titlefont = list(family = "Courier New, monospace",
size = 18,
color = "#7f7f7f "))) %>%
layout(showlegend = FALSE)
p <- subplot(p1, p2, titleX = TRUE, titleY = TRUE)
pp <- plot_ly() %>%
add_trace(x = c("2017-01-01", "2017-02-10", "2017-03-20"),
y = c("A", "B", "C"),
z = c(1, 1000, 100000),
name = "z",
type = "scatter3d") %>%
layout(scene = list(aspectratio = list(x = 1, y = 1, z = 1),
camera = list(center = list(x = 0, y = 0, z = 0),
eye = list(x = 1.96903462608, y = -1.09022831971, z = 0.405345349304),
up = list(x = 0, y = 0, z = 1)),
dragmode = "turntable",
xaxis = list(title = "",
type = "date"),
yaxis = list(title = "",
type = "category"),
zaxis = list(title = "",
type = "log"),
annotations = list(list(showarrow = F,
x = "2017-01-01",
y = "A",
z = 0,
text = "Point 1",
xanchor = "left",
xshift = 10,
opacity = 0.7),
list(x = "2017-02-10",
y = "B",
z = 4,
text = "Point 2",
textangle = 0,
ax = 0,
ay = -75,
font = list(color = "black", size = 12),
arrowcolor = "black",
arrowsize = 3,
arrowwidth = 1,
arrowhead = 1),
list(x = "2017-03-20",
y = "C",
z = 5,
ax = 50,
ay = 0,
text = "Point 3",
arrowhead = 1,
xanchor = "left",
yanchor = "bottom"))),
xaxis = list(title = "x"),
yaxis = list(title = "y"))
pdata <- mtcars %>%
filter(am==1 & gear==4)
p <- plot_ly(data,
x = ~wt,
y = ~mpg,
type = 'scatter',
mode = 'markers',
marker = list(size = 10)) %>%
add_annotations(x = data$wt,
y = data$mpg,
text = rownames(data),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5,
ax = 20,
ay = -40,
font = list(color = '#264E86', family = 'sans serif', size = 14))
pp <- plot_ly() %>%
add_markers(x = 1,
y = 1,
showlegend = F) %>%
add_markers(x = 1,
y = 2,
showlegend = F) %>%
add_markers(x = 1,
y = 3,
showlegend = F) %>%
add_annotations(x=1,
y=1,
xref = "x",
yref = "y",
text = "Right Anchor",
xanchor = 'right',
showarrow = F) %>%
add_annotations(x=1,
y=2,
xref = "x",
yref = "y",
text = "Center Anchor",
xanchor = 'center',
showarrow = F) %>%
add_annotations(x=1,
y=3,
xref = "x",
yref = "y",
text = "Left Anchor",
xanchor = 'left',
showarrow = F)
pPara mostrar un texto arbitrario en el gráfico, puede usar texttemplate, que es una cadena de plantilla utilizada para representar la información, y anulará textinfo.
p <- plot_ly(type='scatterternary',
a = c(3, 2, 5),
b = c(2, 5, 2),
c = c(5, 2, 2),
mode = "markers+text",
text = c("A", "B", "C"),
texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
textposition = "bottom center",
textfont = list(family= "Times",
size= c(18, 21, 20),
color= c("IndianRed", "MediumPurple", "DarkOrange")))
pp <- plot_ly() %>%
add_trace(type='funnel',
name = 'Montreal',
orientation = "h",
y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
x = c(100, 60, 40, 20),
textposition = "inside",
texttemplate = "%{y| %a. %_d %b %Y}") %>%
add_trace(type='funnel',
name = 'Vancouver',
orientation = "h",
y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
x = c(90, 70, 50, 10),
textposition = "inside",
textinfo = "label") %>%
layout(yaxis = list(type= 'date'))
pp<- plot_ly() %>%
add_markers(x = 0.5,
y = 1,
showlegend = F) %>%
add_annotations(x= 0.5,
y= 1,
xref = "paper",
yref = "paper",
text = "<b>paper reference = [0.5, 1]</b>",
showarrow = F) %>%
add_annotations(x= 0.5,
y= 1,
xref = "x",
yref = "y",
text = "x + y reference = [0.5, 1]",
showarrow = T,
ax = 20,
ay = -40) %>%
layout(xaxis = list(zeroline = F),
yaxis = list(zeroline = F))
pp <- plot_ly(x = c(1, 2, 3, 4),
y = c(1, 4, 9, 16),
name = TeX("\\alpha_{1c} = 352 \\pm 11 \\text{ km s}^{-1}")) %>%
add_trace(x = c(1, 2, 3, 4),
y = c(0.5, 2, 4.5, 8),
name = TeX("\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}")) %>%
layout(xaxis = list(title = TeX("\\sqrt{(n_\\text{c}(t|{T_\\text{early}}))}")),
yaxis = list(title = TeX("d, r \\text{ (solar radius)}"))) %>%
config(mathjax = 'cdn')
prequire(random)
n=250
t=12
x = c(rep(0:(n-1), times=t))
y = c(rep(0:(t-1), each=n))
z = randomNumbers(n=n*t, min=1, max=10, col=n)
text = c(replicate(t*n, sample(c("A","T","G","C"), 1)))
steps = list()
for (e in c(0:(n-30))){
step <- list(list(args = list('xaxis.range', c(-0.5 + e, 30.5 + e)),
method = "relayout",
label = e,
value = e))
steps[e] <- step
}
p <- plot_ly() %>%
add_trace(type='heatmap',
z = z) %>%
add_trace(mode = "text",
text = text,
type = "scattergl",
textfont = list(size = 20),
x = x,
y = y) %>%
layout(xaxis = list(range = c(-0.5, 30.5),
showline = F,
zeroline = F,
showgrid = F),
yaxis = list(showline = F,
zeroline = F,
showgrid = F),
sliders=list(list(active = 0,
steps = steps)))
pPor defecto, los gráficos 3D de Plotly muestran lÃneas llamadas spikelines mientras se desplaza sobre un punto. Estas lÃneas se proyectan desde el punto de desplazamiento a cada uno de los planos normales de los tres ejes y luego se extienden desde esos puntos de datos de proyección hasta los lÃmites de la pared de los planos.
data<- mtcars %>%
mutate(am = case_when(am == 0 ~'Automatic',
am == 1 ~'Manual')) %>%
mutate(am = as.factor(.$am))
p <- plot_ly(data,
x = ~wt,
y = ~hp,
z = ~qsec,
opacity = 0.8,
color = ~am,
colors = c('#BF382A', '#0C4B8E')) %>%
add_markers() %>%
layout(scene = list(xaxis = list(spikecolor = '#a009b5',
spikesides = FALSE,
spikethickness = 6),
yaxis = list(spikecolor = '#a009b5',
spikesides = FALSE,
spikethickness = 6),
zaxis = list(spikecolor = '#a009b5',
spikethickness = 6)))
pp1 <- plot_ly(z = ~volcano,
scene='scene1',
lighting = list(ambient = 0.2)) %>%
add_surface(showscale=FALSE)
p2 <- plot_ly(z = ~volcano,
scene='scene2',
lighting = list(ambient = 0.9)) %>%
add_surface(showscale=FALSE)
p <- subplot(p1, p2) %>%
layout(title = "Ambient Lighting",
grid = list(rows = 1, columns = 2, pattern = 'independent'),
scene = list(domain=list(column = 0), aspectmode='cube'),
scene2 = list(domain=list(column = 1), aspectmode='cube'))
pp1 <- plot_ly(z = ~volcano,
scene='scene1',
lighting = list(roughness = 0.1)) %>%
add_surface(showscale=FALSE)
p2 <- plot_ly(z = ~volcano,
scene='scene2',
lighting =
list(roughness = 0.9)) %>%
add_surface(showscale=FALSE)
p <- subplot(p1, p2) %>%
layout(title = "Roughness",
grid = list(rows = 1, columns = 2, pattern = 'independent'),
scene = list(domain=list(column = 0), aspectmode='cube'),
scene2 = list(domain=list(column = 1), aspectmode='cube'))
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 October 21, 2020, from https://plotly.com/r/