你好,我有下面的代码来构建一个子图。我想对图例进行分组,以便只有"spot“和"avg base”出现一次。我已经用legendgroup尝试过了,但是不能正常工作。
mom.base <- mtcars[c(1,2,3),c(1,2,3)]
spt <- mtcars[c(1,2,3),c(4,5,6)]
mom.base <- as.xts(mom.base, order.by = as.Date(c("2017-01-01","2017-01-02","2017-01-03")))
spt <- as.xts(spt, order.by = as.Date(c("2017-01-01","2017-01-02","2017-01-03")))
plot_list <- NULL
for (i in 1:3){
mom.plot <- mom.base[,i]
spt.plot <- spt[,i]
df <- cbind(mom.plot,spt.plot)
colnames(df) <- c("avgbase","spot")
p <- plot_ly((fortify(df)), x = ~factor(Index), y = ~ spot,
type = 'scatter', mode = 'lines', name = 'spot') %>%
add_trace(x = ~factor(Index), y = ~ avgbase, name = "avg base", line = list(dash = "dash")) %>%
layout(title = "test",
xaxis = list(title = ''),
yaxis = list(title = names(mom.plot)))
plot_list[[i]] = p
}
subplot(plot_list, nrows=1, titleY = TRUE, shareX = TRUE)

发布于 2017-08-24 23:49:05

归根结底是:
p1 <- df[[1]] %>%
group_by(type) %>%
plot_ly(x=~factor(Index), y = ~value, color = ~type, colors = c("#132B43", "#56B1F7"), type = 'scatter', mode = 'lines')
p2 <- df[[2]] %>%
group_by(type) %>%
plot_ly(x=~factor(Index), y = ~value, color = ~type, colors = c("#132B43", "#56B1F7"), type = 'scatter', mode = 'lines', showlegend = F)
p3 <- df[[3]] %>%
group_by(type) %>%
plot_ly(x=~factor(Index), y = ~value, color = ~type, colors = c("#132B43", "#56B1F7"), type = 'scatter', mode = 'lines', showlegend = F)
subplot(p1,p2,p3,nrows=1,titleY = TRUE, shareX = TRUE, margin = c(0.085,0.01,0.1,0.1))https://stackoverflow.com/questions/45862240
复制相似问题