【问题标题】:How can I create 2 separate plot on the same page with 2 different y variables within my function如何在我的函数中使用 2 个不同的 y 变量在同一页面上创建 2 个单独的图
【发布时间】: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"))


【问题讨论】:

    标签: r for-loop ggplot2


    【解决方案1】:

    试试这个。一些细节。您可以使用split() by player 在列表中拆分数据框,然后在函数中设置方案图。由于您希望每个玩家有两个地块,您可以在函数内构建它们并使用patchwork 您可以合并它。最后,您可以使用lapply() 应用该函数来创建避免循环的图。最后,使用带有循环的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()
    

    pdf 文件中的输出将如下所示:

    【讨论】:

    • 你知道为什么它说 G2 不能添加到 ggplot Error: Can't add G2 to a ggplot object. 另外,x 是从哪里来的,我认为这将是我的数据集。感谢您的帮助,非常感谢
    • @pat28dh 尝试重新启动 R 并仅运行解决方案中的代码。看起来有冲突,还要确保 patchwork 已加载!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多