首页 > 解决方案 > 带有ggplot和分组图例的情节子图?

问题描述

我正在尝试安排所有情节,即在plotly子情节中有共同图例的条形图。我已经使用创建了单独的子图,但似乎ggplotlyhide_legend()没有解决我的问题。使用 plotly 更容易做同样的事情,但我不得不忘记 ggplot。任何人都可以在这件事上帮助我吗?

这是描述我的问题的代码:

library(dplyr)
library(ggplot2)
library(plotly)
data <- dget("loan_pred.data")
factor_col_names <- names(data)[map_lgl(data,is.factor)]
n<- 1
create_barplot_plotly <- function(feature){
  g <- ggplot(data) +
        aes_string(x=feature)+
        ggtitle(paste(feature))+
        geom_bar(aes(fill=Loan_Status),position = "fill")+
        ylim(0,1+0.10)+
        ylab("proportion") + theme_bw()

  g <- ggplotly(g)

  if (n %% length(factor_col_names)!=1){
      g <- hide_legend(g) 
  }

  n <<- n +1
  return(g)

}  
plotly::subplot(map(factor_col_names,create_barplot_plotly),
                  nrows = 3,margin = 0.06,titleX = TRUE,titleY = TRUE)%>%
                  layout(title="BoxPlots")

我已经使用Google Drive上的 dput 上传了数据并编辑了代码以减少依赖关系。

标签: rggplot2r-plotlyggplotly

解决方案


推荐阅读