r - 带有ggplot和分组图例的情节子图?
问题描述
我正在尝试安排所有情节,即在plotly
子情节中有共同图例的条形图。我已经使用创建了单独的子图,但似乎ggplotly
并hide_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 上传了数据并编辑了代码以减少依赖关系。
解决方案
推荐阅读
- python-3.x - 完整的 DataFrame 缺少步骤 python
- python-3.x - 请帮助解决python中的装饰器问题
- python - 使用 python 'aiohttp' 发送文件会产生“解析正文时出错”
- arrays - 如何从 C 中的 U8 数组中获取 U16 变量?
- angular - 如何在组件中使用 Promise 在视图中显示警报
- debian - 如何修复多通道错误:列表失败:无法连接到多通道套接字
- ios - 滑动 PageViewController 时如何在 ViewController 中更新 UIPageControl
- spring-boot - 简单的 Mockito 单元测试失败
- php - PHP代码,最后一行不会没有错误运行
- kubernetes - MySQL Kubernetes Pod 进入 CrashLoopBackOff 并且无法解密具有静态加密的 .ibd。MySQL pod 不会回到运行状态