Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在同一页上创建两个单独的绘图,在函数中使用两个不同的y变量_R_For Loop_Ggplot2 - Fatal编程技术网

R 如何在同一页上创建两个单独的绘图,在函数中使用两个不同的y变量

R 如何在同一页上创建两个单独的绘图,在函数中使用两个不同的y变量,r,for-loop,ggplot2,R,For Loop,Ggplot2,所以我有一个4个变量的数据框,使用for循环,我想写一个函数,为每个玩家创建一个页面,x轴上有年份,y轴上有一个最大摄氧量图,y轴上有一个hp图 在底部,您可以找到我试图添加到此代码中的第二个ggplot,但它不起作用。谢谢你的帮助 tv<- data.frame( player = c("p1","p2","p3", "p1", "p2", "p3"), ye

所以我有一个4个变量的数据框,使用for循环,我想写一个函数,为每个玩家创建一个页面,x轴上有年份,y轴上有一个最大摄氧量图,y轴上有一个hp图

在底部,您可以找到我试图添加到此代码中的第二个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"))




电视试试这个。一些细节。您可以使用播放器的
split()
在列表中拆分数据帧,然后在函数中设置scheme plot。由于您希望每个播放器有两个绘图,因此可以在函数内部构建它们,并使用
补丁
将其合并。最后,您可以使用
lappy()
应用该函数创建避免循环的绘图。最后,使用带有循环的
pdf()
函数,您可以使用我在最后显示的图像创建pdf。下面是带有内置函数和流程的代码:

library(ggplot2)
library(patchwork)
#Data
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)
)
#List
List <- split(tv,tv$player)
#Function
myfunplot <- function(x)
{
  #Plot 1
  G1 <- ggplot(x, aes(year, vo2max,group=1)) +
           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))+
    ggtitle(paste0('vo2max for ',unique(x$player)))
  #Plot 2
  G2 <- ggplot(x, aes(year, hp,group=1)) +
    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))+
    ggtitle(paste0('hp for ',unique(x$player)))
  #Compose
  GF <- G1+G2
  return(GF)
}
#Apply
Listplot <- lapply(List, myfunplot)
#Prepare to export
pdf('Myplots.pdf',width = 14)
for(i in c(1:length(Listplot)))
{
  plot(Listplot[[i]])
}
dev.off()
库(ggplot2)
图书馆(拼凑)
#资料

Tv你知道为什么它说G2不能添加到ggplot
错误:不能将G2添加到ggplot对象。
另外,x从何而来,认为它是我的数据集。非常感谢你的帮助appreciated@pat28dh尝试重新启动R并仅运行解决方案中的代码。这看起来像是冲突,还请确保加载了
补丁