R语言使用plotly绘图时如何移除重复的Aa图例
R语言Plotly绘图双重图例移除方案
问题原因
带Aa标识的第二份图例是add_text生成的文本图层默认带的图例映射,你在绘图时将城市颜色同时映射给了标记点和文本两类图层,因此生成了两套独立图例。
解决方法
在add_text的参数中添加showlegend = FALSE即可关闭文本图层的图例展示,同时可以优化初始绘图逻辑避免冗余参数,修改后可运行的完整代码如下:
library(plotly) city = c("paris", "lyon", "lyon", "marseille", "lille", "toulouse", "bordeaux") start = c("2018-08-04", "2018-07-25", "2018-07-30", "2018-07-29", "2018-08-03", "2018-08-04", "2018-08-03") max = c(36.4, 37.2, 38.4, 37.4, 36.7, 34.9, 34.8) duration = c(4, 3, 8, 10, 5, 4, 3) tab = data.frame(city, start, duration, max) tab$duration = as.integer(tab$duration) t <- list( family = "sans serif", size = 14, color = toRGB("grey50")) # 初始调用仅保留基础映射,避免重复设置图层类型 bubbleplot <- plot_ly(tab, x = ~start, y = ~max, color = ~city) bubbleplot = bubbleplot %>% add_markers( text = ~paste(duration, "jours"), marker = list(size = ~duration*4, opacity = 0.7, sizemode = "diameter") ) # 核心修改:添加showlegend = FALSE关闭文本图层的图例输出 bubbleplot <- bubbleplot %>% add_text( textfont = t, textposition = "inside", showlegend = FALSE ) bubbleplot
内容的提问来源于stack exchange,提问作者Nathancia971
相关产品推荐
相关产品推荐

