给出以下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)
我几乎可以得到想要的绘图(现在忽略了所有警告)。
但是,我无法弄清楚如何制作图例以保持动画大小。它出现在开始阶段,在按下播放按钮之前,但是一旦按下播放键,它就会消失。
因此,当我运行动画时,静态图数据点将从子图中消失。有什么提示吗?
答案 0 :(得分:1)
这有效。伪传奇消失的原因是您只在第一帧使用它。我在这里所做的是添加所有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")))