我使用 plot_ly 绘制数据图表,只需要在特定数据点(2030 年和 2050 年)添加注释。
这是剧情部分的代码:
res<-data_plot%>%
plot_ly(
x=~year,
# frame=~year,
y=~value,
color=~state,
# colors = colors,
# hoverinfo = "text",
type = 'scatter',
mode = 'lines',
showlegend = TRUE
)%>%
add_trace(
x =2030,
type = 'scatter',
mode = 'lines',
showlegend = FALSE,
line = list(color = 'rgb(205, 12, 24)',
width = 1,
dash = 'dash'),
name = '')%>%
add_trace(
x =2050,
type = 'scatter',
mode = 'lines',
showlegend = FALSE,
line = list(color = 'rgb(205, 12, 24)',
width = 1,
dash = 'dash'),
name = '')
我试图在特定点将我的数据子集添加到 add_trace:
data_annot <- data_plot[data_plot$year %in% c(2030, 2050),]
res <- add_trace(res, x=data_annot$year, y=data_annot$value, color=~state, type="scatter", mode="markers")
但它给了我这个错误:我该如何解决这个问题?
Error:
! Tibble columns must have compatible sizes.
• Size 4: Columns `x` and `y`.
• Size 54: Column `color`.
ℹ Only values of size one are recycled.
这就是我的情节现在的样子: