【问题标题】:Vertically combing multiple bar plots垂直梳理多个条形图
【发布时间】:2019-09-01 16:24:35
【问题描述】:

我正在尝试使用垂直分组条形图,共享它们的 x 轴。

我想用Rplotlysubplot来解决这个问题,但遇到了一个问题,我希望这里有人可以解决。

这里是包含 28 个组的示例数据,我在每个组中创建了一个超过 4 个家庭的条形图,然后尝试使用 plotly::subplot 垂直组合它们:

set.seed(1)
df <- data.frame(group = paste0("G",unlist(lapply(1:28,function(i) rep(i,4)))),
                 family = paste0("F",rep(1:4,28)),
                 log2n = log2(as.integer(runif(4*28,0,30))+1),
                 stringsAsFactors = F)

创建条形图列表:

library(plotly)
library(dplyr)

groups <- unique(df$group)
y.range <- c(0,max(df$log2n))
plot.list <- lapply(1:length(groups),function(g){
  group.df <- dplyr::filter(df,group == groups[g])
  plot_ly(x=group.df$family,y=group.df$log2n,type='bar',name=group.df$family,color=group.df$family,showlegend=(g==length(groups))) %>%
    layout(yaxis=list(range=y.range))
})

如果我尝试:

plotly::subplot(plot.list,shareX=T,nrows=length(plot.list))

我得到:

所以这似乎是某种溢出。

我逐渐减少了 plot.list 中运行 subplot 的地块数量,当达到 19 时,它似乎停止“溢出”:

plotly::subplot(plot.list[1:19],shareX=T,nrows=19) 

是否有希望获得所有 28 个条形图而不会溢出?

非常感谢

【问题讨论】:

    标签: r bar-chart plotly subplot


    【解决方案1】:

    我会使用ggplot 生成图形,然后使用适当的大小参数将其转换为plotly(或将其保存为图片文件)。

    library(plotly)
    library(tidyverse)
    
    g <- ggplot(df, 
           aes(x = family, y = log2n, fill = family)) +
        geom_bar(stat = 'identity') +
        facet_wrap(~group, ncol = 1) +  
        theme_minimal() + 
        theme(legend.position = "none") 
    
    ggsave(g, file = "temp.png", width = 4, height = 40)
    ggplotly(g, width = 400, height = 4000)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2016-10-18
      • 2015-05-29
      • 1970-01-01
      • 2019-09-07
      相关资源
      最近更新 更多