R 在一页上放置多个绘图
我使用for循环生成了图,但现在我无法将它们全部放在一页上。我的代码是:R 在一页上放置多个绘图,r,for-loop,ggplot2,R,For Loop,Ggplot2,我使用for循环生成了图,但现在我无法将它们全部放在一页上。我的代码是: 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 = "
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)
}
我正在处理的数据集可以在
我是R的新手,所以任何帮助都将不胜感激。
谢谢您可以尝试在每个
季节使用facet
:
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')