R在for循环内部生成图不流动尺寸?

时间:2018-10-02 09:22:39

标签: r ggplot2 plot

我写了一个for循环来制作图表矩阵,但是我写了如下代码

ngOnInit() {
  this._dashService.getOrder().subscribe(order => {
    this.orders = order;
    console.log(this.orders);
  })
}

仅显示单个图形,而不显示我想要的矩阵

1 个答案:

答案 0 :(得分:0)

您正在使用ggboxplot中的ggpubr,它基于ggplot2,而grid是基于grid图形系统的。不幸的是,base图形不能与par R图形一起使用,因此设置grid也不起作用。要安排由库构建在grid.arrange之上的图,请使用gridExtra包中的library(ggpubr) data("ToothGrowth") df <- ToothGrowth grobsList <- list() for (i in c(1:4)) { p <- ggboxplot(df, x = "dose", y = "len", width = 0.8) grobsList <- c(grobsList, list(p)) } gridExtra::grid.arrange( grobs = grobsList, ncol = 2, nrow = 2) 。请参见下面的示例:

marrangeGrob

您也可以尝试使用library(ggpubr) data("ToothGrowth") df <- ToothGrowth grobsList <- list() for (i in c(1:7)) { p <- ggboxplot(df, x = "dose", y = "len", width = 0.8) grobsList <- c(grobsList, list(p)) if(length(grobsList) == 4) { # print the result gridExtra::grid.arrange( grobs = grobsList, ncol = 2, nrow = 2) grobsList <- list() # reset the list containing the plots } } # print the remaining plots if(length(grobsList) > 0) { gridExtra::grid.arrange( grobs = grobsList, ncol = 2, nrow = 2) } ,但它可能会在当前图形窗口之外创建绘图。

创建所需数量的图后,您还可以在循环内渲染图:

{ cancel-account: "Please don't do it!" }