【发布时间】:2020-10-22 21:41:52
【问题描述】:
所以我有一个使用 for 循环的 4 个变量的数据框,我想编写一个函数,为每个玩家创建 1 个页面,x 轴为年份,1 个图表,y 轴为 vo2max,1以 hp 为 y 轴的图表。
在底部,您可以找到我尝试添加到此代码中的第二个 ggplot,但它不起作用。谢谢你的帮助。
tv<- data.frame(
player = c("p1","p2","p3", "p1", "p2", "p3"),
year = c("2010", "2010", "2010", "2015", "2015", "2015"),
vo2max = c(50, 52, 52, 53, 54, 56),
hp = c(100, 100, 105, 103, 102, 105)
)
vohp <- function(id){
vohp1 <- tv %>% filter(player == id)
ggplot(vohp1, aes(year, vo2max) +
geom_point() +
geom_line() +
scale_y_continuous(label = scales::number_format(accuracy = 5))+
theme(axis.text.x = element_text(size= 6, angle = 90), strip.text = element_text(size = 6))+
ggsave(paste0("~/Desktop/rtest//", id, "testttttt.pdf"))
}
for(id in unique(tv$player)){
vohp(id)
}
#supplemental part that was not working into the code
vohp2 <- tv %>% filter(player == id)
ggplot(vohp2, aes(year, hp, group = id)) +
geom_point() +
geom_line() +
scale_y_continuous(label = scales::number_format(accuracy = 5))+
theme(axis.text.x = element_text(size= 6, angle = 90), strip.text = element_text(size = 6))+
ggsave(paste0("~/Desktop/rtest//", id, "testttttt.pdf"))
【问题讨论】: