使用子图组合静态和动画绘图对象
Combining static and animated plotly objects using subplot
给出以下 data.frame
data <- structure(list(
a = c(3.022210021321, 3.31806778755904, 3.34379454984061, 3.47242836124846, 3.55604033866356, 1.11199792191451, 1.24063173332236, 1.31781202016707, 1.30494863902628, 1.3692655447302, 1.07983946906255, 1.2084732804704, 1.40142399758216, 1.60723809583472, 1.64582823925707),
b = c(2.64027979608152, 2.79483009168741, 2.90522315997732, 3.08185206924119, 2.86106593266136, 0.653204566863006, 0.697361794178973, 0.67528318052099, 0.653204566863006, 0.697361794178973, 2.06623584097395, 2.28702197755379, 2.48572950047564, 2.72859425071346, 2.77275147802942),
c = c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L),
d = c(1.16264425026133, 1.16264425026133, 1.16264425026133, 1.16264425026133, 1.16264425026133, 1.48373054411498, 1.48373054411498, 1.48373054411498, 1.48373054411498, 1.48373054411498, 3.35362520562369, 3.35362520562369, 3.35362520562369, 3.35362520562369, 3.35362520562369),
e = c(2015L, 2016L, 2017L, 2018L, 2019L, 2015L, 2016L, 2017L, 2018L, 2019L, 2015L, 2016L, 2017L, 2018L, 2019L),
f = c("X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Z", "Z", "Z", "Z", "Z"),
h = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L), .Label = c("low", "mid", "high"), class = c("ordered", "factor"))),
row.names = c(NA, -15L),
class = "data.frame")
和以下代码片段
library(plotly)
p <- data %>% plot_ly(
x = ~a,
y = ~b,
size = ~c,
sizes = c(100, 850),
color = ~d,
colors = "YlOrRd",
alpha = 0.365,
frame = ~e,
text = ~paste0("Info: ", f),
hoverinfo = "text",
type = 'scatter',
mode = 'markers') %>%
add_text(textfont = list(size = 10, color = "black"), textposition = "top", text=~f, showlegend = F)
legend.plot <- plot_ly() %>%
add_markers(x = 1,
y = seq_len(length(unique(data$c))),
size = sort(unique(data$c)),
showlegend = F,
color = I("black"),
marker = list(sizeref=0.1, sizemode="area")) %>%
layout(
annotations = list(
list(x = 1.2,
y = 0.4,
text = "Size by: c",
showarrow = F,
xref='paper',
yref='paper')),
xaxis = list(
zeroline=F,
showline=F,
showticklabels=F,
showgrid=F),
yaxis=list(
side = "right",
range = c(0,10),
showgrid=F,
zeroline=F,
tickmode = "array",
tickvals = seq_len(length(unique(data$c))),
ticktext = c("low","mid","high")))
subplot(p, legend.plot, widths = c(0.85, 0.15), titleX=TRUE, titleY=TRUE) %>%
config(displayModeBar = F) %>%
colorbar(title = "Color by: d", x = 0.9, y = 1)
我几乎得到了我想要的情节(暂时忽略沿途的警告)。
但是,我无法弄清楚如何在动画期间保留尺寸图例。它出现在初始阶段,在按下播放按钮之前,但我一按播放,它就消失了。
因此当我 运行 动画时,静态绘图数据点从子图中消失。有什么提示吗?
这行得通。你的 pseudo-legend 消失的原因是你只在第一帧有它。我在这里所做的是添加所有 5 年的数据(这就是为什么我有 rep
)并确保 frame
包含在两个图中。
legend.plot <- plot_ly() %>%
add_markers(x = rep(1,15),
y = rep(seq_len(length(unique(data$c))),5),
size = rep(sort(unique(data$c)),5),
showlegend = F,
color = I("black"),
frame = ~data$e,
marker = list(sizeref=0.1, sizemode="area")) %>%
layout(
annotations = list(
list(x = 1.2,
y = 0.4,
text = "Size by: c",
showarrow = F,
xref='paper',
yref='paper')),
xaxis = list(
zeroline=F,
showline=F,
showticklabels=F,
showgrid=F),
yaxis=list(
side = "right",
range = c(0,10),
showgrid=F,
zeroline=F,
tickmode = "array",
tickvals = seq_len(length(unique(data$c))),
ticktext = c("low","mid","high")))
给出以下 data.frame
data <- structure(list(
a = c(3.022210021321, 3.31806778755904, 3.34379454984061, 3.47242836124846, 3.55604033866356, 1.11199792191451, 1.24063173332236, 1.31781202016707, 1.30494863902628, 1.3692655447302, 1.07983946906255, 1.2084732804704, 1.40142399758216, 1.60723809583472, 1.64582823925707),
b = c(2.64027979608152, 2.79483009168741, 2.90522315997732, 3.08185206924119, 2.86106593266136, 0.653204566863006, 0.697361794178973, 0.67528318052099, 0.653204566863006, 0.697361794178973, 2.06623584097395, 2.28702197755379, 2.48572950047564, 2.72859425071346, 2.77275147802942),
c = c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L),
d = c(1.16264425026133, 1.16264425026133, 1.16264425026133, 1.16264425026133, 1.16264425026133, 1.48373054411498, 1.48373054411498, 1.48373054411498, 1.48373054411498, 1.48373054411498, 3.35362520562369, 3.35362520562369, 3.35362520562369, 3.35362520562369, 3.35362520562369),
e = c(2015L, 2016L, 2017L, 2018L, 2019L, 2015L, 2016L, 2017L, 2018L, 2019L, 2015L, 2016L, 2017L, 2018L, 2019L),
f = c("X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Z", "Z", "Z", "Z", "Z"),
h = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L), .Label = c("low", "mid", "high"), class = c("ordered", "factor"))),
row.names = c(NA, -15L),
class = "data.frame")
和以下代码片段
library(plotly)
p <- data %>% plot_ly(
x = ~a,
y = ~b,
size = ~c,
sizes = c(100, 850),
color = ~d,
colors = "YlOrRd",
alpha = 0.365,
frame = ~e,
text = ~paste0("Info: ", f),
hoverinfo = "text",
type = 'scatter',
mode = 'markers') %>%
add_text(textfont = list(size = 10, color = "black"), textposition = "top", text=~f, showlegend = F)
legend.plot <- plot_ly() %>%
add_markers(x = 1,
y = seq_len(length(unique(data$c))),
size = sort(unique(data$c)),
showlegend = F,
color = I("black"),
marker = list(sizeref=0.1, sizemode="area")) %>%
layout(
annotations = list(
list(x = 1.2,
y = 0.4,
text = "Size by: c",
showarrow = F,
xref='paper',
yref='paper')),
xaxis = list(
zeroline=F,
showline=F,
showticklabels=F,
showgrid=F),
yaxis=list(
side = "right",
range = c(0,10),
showgrid=F,
zeroline=F,
tickmode = "array",
tickvals = seq_len(length(unique(data$c))),
ticktext = c("low","mid","high")))
subplot(p, legend.plot, widths = c(0.85, 0.15), titleX=TRUE, titleY=TRUE) %>%
config(displayModeBar = F) %>%
colorbar(title = "Color by: d", x = 0.9, y = 1)
我几乎得到了我想要的情节(暂时忽略沿途的警告)。
但是,我无法弄清楚如何在动画期间保留尺寸图例。它出现在初始阶段,在按下播放按钮之前,但我一按播放,它就消失了。
因此当我 运行 动画时,静态绘图数据点从子图中消失。有什么提示吗?
这行得通。你的 pseudo-legend 消失的原因是你只在第一帧有它。我在这里所做的是添加所有 5 年的数据(这就是为什么我有 rep
)并确保 frame
包含在两个图中。
legend.plot <- plot_ly() %>%
add_markers(x = rep(1,15),
y = rep(seq_len(length(unique(data$c))),5),
size = rep(sort(unique(data$c)),5),
showlegend = F,
color = I("black"),
frame = ~data$e,
marker = list(sizeref=0.1, sizemode="area")) %>%
layout(
annotations = list(
list(x = 1.2,
y = 0.4,
text = "Size by: c",
showarrow = F,
xref='paper',
yref='paper')),
xaxis = list(
zeroline=F,
showline=F,
showticklabels=F,
showgrid=F),
yaxis=list(
side = "right",
range = c(0,10),
showgrid=F,
zeroline=F,
tickmode = "array",
tickvals = seq_len(length(unique(data$c))),
ticktext = c("low","mid","high")))