微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我无法将我的图绘制到单个网格中,请帮助更正我的代码

如何解决我无法将我的图绘制到单个网格中,请帮助更正我的代码

我有 11 个图并使用循环函数来绘制它们,请参阅下面的代码。但是,我无法让它们仅显示在 1 页或更少的页面中。情节实在是太大了。我正在使用 R 软件并在 RMarkdown 中编写我的工作。我几乎花了整整一周的时间试图解决这个问题。

  group_by(Firm_category) %>%
  doo(
    ~ggBoxplot(
      data =.,x = "Means.type",y = "means",fill ="grey",palette = "npg",legend = "none",ggtheme = theme_pubr()
      ),result = "plots"
  )
graph3

# Add statistical tests to each corresponding plot

Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
  graph3.i <- graph3$plots[[i]] + 
    labs(title = Firm_category[i]) +
    stat_pvalue_manual(stat.test[i,],label = "p.adj.signif")
  print(graph3.i)
}

#output3.long data sample below as comments
#Firm_category  billmonth  Means.type  means
#Agric          1           Before       38.4444
#Agric          1           After        51.9

完整数据在我的github上:https://github.com/Fridahnyakundi/Descriptives-in-R/blob/master/Output3.csv代码打印所有图形,但大约为 4 页。我想将它们分组到一个网格中。我已经尝试在最后一个大括号之前添加下面所有这些代码,但没有任何效果,请帮帮我。

library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11],nrow = 4,ncol = 3))

library(ggpubr)
print(ggarrange(graph3.i[1:11],ncol = 3))

我也尝试了 gridExtra 命令(它们似乎都在做同样的事情)。我是一个错误的人,我想这与我的清单有关。我在这里阅读了很多类似的工作,有些建议

dev.new() 
dev.off()

我仍然没有明白他们在做什么。但是添加它们中的任何一个都会导致我的代码停止。 我尝试定义我的 'for' 循环函数,说将其称为 'XX',然后调用它来制作图表列表,但它返回了 NULL 输出

我尝试定义一个空列表(正如我在此处阅读的一些答案),然后计算它们以制作一个可以打印的列表,但我遇到了很多错误。 我已经这样做了将近 3 天,非常感谢您帮助解决这个问题。 谢谢!

解决方法

我试图完成你的代码......这有效(但我没有你的'stat.test'对象)。基本上,我在循环中添加了一个 graph3.i <- list() 并替换了 graph3.i .. 这是你想做的吗?


library(magrittr)
library(dplyr)
library(rstatix)
library(ggplot2)
library(ggpubr)
data <- read.csv(url('http://raw.githubusercontent.com/Fridahnyakundi/Descriptives-in-R/master/Output3.csv'))
graph3 <- data %>% group_by(Firm_category) %>%
  doo(
    ~ggboxplot(
      data =.,x = "Means.type",y = "means",fill ="grey",palette = "npg",legend = "none",ggtheme = theme_pubr()
    ),result = "plots"
  )
graph3

# Add statistical tests to each corresponding plot
graph3.i <- list()
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
  graph3.i[[i]] <- graph3$plots[[i]] + 
    labs(title = Firm_category[i]) # +
    # stat_pvalue_manual(stat.test[i,],label = "p.adj.signif")
  print(graph3.i)
}


library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11],nrow = 4,ncol = 3))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。