用子图循环遍历R Plotly并隐藏除一个以外的所有图例 [英] Looping through R Plotly with subplot and hiding all legend except one
问题描述
我需要循环遍历因子的i次迭代,并且每个因子都需要在子图中作为一个图进行绘制.我想做的是在第一个迭代栏中隐藏图例,并使用legendgroup将所有图例捆绑在一起.这是我到目前为止所做的:
I need to loop through i iteration of factors, and each factor needs to be plotted as one plot in a subplot. What I would like to do is hiding the legend for every iteration bar the first one, and use legendgroup to tie all the legends together. This is what I have done so far:
library(plotly)
library(dplyr)
mtcars %>%
mutate(vs = as.factor(vs)) %>%
group_split(cyl) %>%
lapply(function(i) {
#show.legend <- ifelse(i == 1, TRUE, FALSE)
show.legend <- if(i == 1) {TRUE} else {FALSE}
plot_ly(
data = i
,x = ~gear
,y = ~mpg
,color = ~vs
,type = "bar"
,legendgroup = ~vs
) %>%
layout(
barmode = "stack"
,showlegend = show.legend
)
}) %>%
subplot(
nrows = NROW(.)
,shareX = TRUE
,shareY = TRUE
,titleX = TRUE
,titleY = TRUE
,margin = 0.05
)
但是这会产生错误并且没有图例:
However this produces an error and no legend:
Warning messages:
1: In if (i == 1) { :
the condition has length > 1 and only the first element will be used
如果我使用 show.legend<-ifelse(i == 1,TRUE,FALSE)
(上面注释),我会得到多个图例,而不仅仅是一次.
If I use show.legend <- ifelse(i == 1, TRUE, FALSE)
(commented out above), I get multiple legends instead of just once.
我知道我可以执行以下操作,但是我需要循环执行此操作.
I am aware I could do the below, but I need to this in a loop.
p1 <- plot_ly(blah, showlegend = TRUE)
p2 <- plot_ly(blah, showlegend = FALSE)
P3 <- plot_ly(blah, showlegend = FALSE)
subplot(p1,p2,p3)
我认为我没有正确调用i迭代.作为另一种选择,我尝试了 case_when
:
I believe I am not calling the i iteration properly. As another option I tried case_when
:
show.legend <- case_when(
i == 1 ~ TRUE
,i != 1 ~ FALSE
)
但是,它产生的结果与ifelse相同.
However this produces the same result as ifelse.
推荐答案
您的代码中存在两个问题:
There are two issues in your code:
-
i
不是1:3
,而是您通过lapply进行迭代的当前小技巧(请参见下面的seq_along
).这就是为什么您得到警告的原因:
i
is not1:3
but your current tibble you are iterating through via lapply (seeseq_along
below). That is why you get the warning:
如果if(i == 1){:条件的长度> 1,并且只有第一个元素将被使用
In if (i == 1) { : the condition has length > 1 and only the first element will be used
-
showlegend
必须是plot_ly
的参数,而不是layout
的参数,因为子图始终采用<强>一个地块.参见?subplot
及其参数which_layout
.
showlegend
needs to be an argument toplot_ly
not tolayout
because subplot always adopts thelayout
from one of its plots. see?subplot
and its argumentwhich_layout
.
在绘图序列的后面找到的布局选项将被覆盖序列中较早的选项
layout options found later in the sequence of plots will override options found earlier in the sequence
这就是我想你所追求的:
Here is what I think you are after:
library(plotly)
library(dplyr)
tibble_list <- mtcars %>%
mutate(vs = as.factor(vs)) %>%
group_split(cyl)
lapply(seq_along(tibble_list), function(i) {
show_legend <- if (i == 1) {TRUE} else {FALSE}
plot_ly(
data = tibble_list[[i]],
x = ~ gear,
y = ~ mpg,
color = ~ vs,
type = "bar",
legendgroup = ~ vs,
showlegend = show_legend
) %>% layout(barmode = "stack")
}) %>% subplot(
nrows = NROW(.),
shareX = TRUE,
shareY = TRUE,
titleX = TRUE,
titleY = TRUE,
margin = 0.05,
which_layout = 1
)
请在此处找到一个官方示例.
Please find an offical example here.
这篇关于用子图循环遍历R Plotly并隐藏除一个以外的所有图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!