0

我使用此答案gpairs_lower中的函数仅显示图矩阵的下三角形。但现在我不知道如何保存结果图。ggpairs

ggpairs保存绘图的常用方法在这里不起作用

gpairs_lower <- function(g){
  g$plots <- g$plots[-(1:g$nrow)]
  g$yAxisLabels <- g$yAxisLabels[-1]
  g$nrow <- g$nrow -1

  g$plots <- g$plots[-(seq(g$ncol, length(g$plots), by = g$ncol))]
  g$xAxisLabels <- g$xAxisLabels[-g$ncol]
  g$ncol <- g$ncol - 1

  g
}

library("GGally")
g <- ggpairs(iris[, 1:4], 
             lower  = list(continuous = "points"),
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag")
     )


png("graph.png", height = 720, width = 720)
gr <- gpairs_lower(g)
print(gr)
dev.off()

## graph.png is not saved

我相信它不起作用,因为与不gpairs_lower返回ggpairs对象相反。ggmatrix

理查德任何帮助将不胜感激。

编辑:现在上面的代码有效!

4

1 回答 1

1

您的代码的问题是默认的高度和宽度单位是像素,所以您保存的是 7x7 像素的图像!!尝试其他值或更改units

png("myPlotMatrix.png", height = 700, width = 700)
g <- ggpairs(iris[, 1:4], 
             lower  = list(continuous = "points"),
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag")
)

g<-gpairs_lower(g)
print(g)
dev.off()

?png

宽度:设备的宽度。

height:设备的高度。

单位:给出高度和宽度的单位。可以是 px(像素,默认值)、in(英寸)、cm 或 mm。

于 2019-02-24T21:34:24.923 回答