在 ggplotly 中,如何使用代码取消选择图例条目?
In ggplotly, how can deselect legend entries with code?
我正在制作一个 ggplotly 图,它定义了具有不同填充颜色的组(A 组或 B 组)。
library(ggplot2)
library(plotly)
data <- data.frame(x = c(1,2,3, 10, 11, 12),
y = c(1,2,3, 10, 11, 12),
group = c(rep("A",3), rep("B",3)))
p <- ggplot(data, aes(x = x, y = y, fill = group))+
geom_point()
ggplotly(p)
我希望其中一个关卡默认不显示,就像我点击图例隐藏关卡一样。
如何以编程方式设置图例,以便默认取消选择 B 组。
受此启发 this question, you can modify each trace with the property visible = 'legendonly
. As r2evans noted, it's not always simple to translate between plot_ly
and ggplotly
. This post 展示了如何修复 ggplotly 对象(如果您已经拥有该对象)并且它对我有用。
gg <- ggplotly(p)
gg <- plotly_build(gg)
gg$x$data[[2]]$visible <- 'legendonly'
gg
我正在制作一个 ggplotly 图,它定义了具有不同填充颜色的组(A 组或 B 组)。
library(ggplot2)
library(plotly)
data <- data.frame(x = c(1,2,3, 10, 11, 12),
y = c(1,2,3, 10, 11, 12),
group = c(rep("A",3), rep("B",3)))
p <- ggplot(data, aes(x = x, y = y, fill = group))+
geom_point()
ggplotly(p)
我希望其中一个关卡默认不显示,就像我点击图例隐藏关卡一样。
如何以编程方式设置图例,以便默认取消选择 B 组。
受此启发 this question, you can modify each trace with the property visible = 'legendonly
. As r2evans noted, it's not always simple to translate between plot_ly
and ggplotly
. This post 展示了如何修复 ggplotly 对象(如果您已经拥有该对象)并且它对我有用。
gg <- ggplotly(p)
gg <- plotly_build(gg)
gg$x$data[[2]]$visible <- 'legendonly'
gg