r - 在一页上放置多个图
问题描述
我使用 for 循环生成了图,但现在我无法将它们全部放在 1 页上。我的代码是:
for (i in seq(2013, 2019, by=1)) {
ipl %>%
filter(season == i) %>%
pivot_longer(cols = c(team1, team2), values_to = 'team_name') %>%
ggplot( aes(x = team_name))+
geom_bar(stat = "count")+
ggtitle(paste("Total Number of matches played by each team in ",i, sep=''))+
coord_flip()+
scale_y_continuous(breaks = seq(0, 20, 2))+
labs(x = "Teams", y= "Number of Matches Played")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title.x =element_text(hjust = 0.5, size = 16),
axis.title.y =element_text(hjust = 0.5, size = 16),
axis.text.x = element_text(size=13))->g
print(g)
}
我正在处理的数据集可以在kaggle上找到
我是 R 新手,所以任何帮助将不胜感激。谢谢
解决方案
您可以尝试为每个使用构面season
:
library(dplyr)
library(ggplot2)
ipl %>%
filter(between(season, 2013, 2019)) %>%
tidyr::pivot_longer(cols = c(team1, team2), values_to = 'team_name') %>%
count(season, team_name) %>%
ggplot(aes(x = team_name, y = n))+
geom_col() +
coord_flip()+
scale_y_continuous(breaks = seq(0, 20, 2))+
labs(x = "Teams", y= "Number of Matches Played")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title.x =element_text(hjust = 0.5, size = 10),
axis.title.y =element_text(hjust = 0.5, size = 16),
axis.text.x = element_text(size=13)) +
facet_wrap(.~season, scales = 'free_y')
推荐阅读
- python - FFTW backwards transform is multiplied by N
- ruby-on-rails - How to find a record through an association?
- react-native - Expo Ref usage and Navigation Container Conflicting
- google-apps-script - 如何仅获取要在文件名中使用的单元格值的日期部分?
- hdfs - How to move deleted HDFS file into the Previous location
- python-3.x - 带有pycharm的Windows 10上python 3.7 / 3.8的dlib(最新版本)安装错误 - 已解决
- powershell - 获取 HTTP 状态代码:使用带有 AzureRm 模块的 PowerShell 创建 Azure 数据工厂触发器时出现 BadRequest
- data-annotations - 使用 efcore .netcore 将关键数据注释分配给我的 razor Web 应用程序中的单独部分类时不起作用
- java-8 - 如何在 Java 7 中将 ZonedDateTime 转换为 XMLGregorianCalendar
- android - Cannot resolve symbol 'cat_item_layout' in Android Studio