首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图案组图R子图

图案组图R子图
EN

Stack Overflow用户
提问于 2017-08-24 20:55:38
回答 1查看 2.1K关注 0票数 1

你好,我有下面的代码来构建一个子图。我想对图例进行分组,以便只有"spot“和"avg base”出现一次。我已经用legendgroup尝试过了,但是不能正常工作。

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

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

使用answer found here回答

归根结底是:

代码语言:javascript
复制
      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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45862240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档