grid.arrange() 플롯을 파일에 저장하는 중
다음을 사용하여 다중 플롯을 표시하려고 합니다.ggplot2다음을 사용하여 정렬grid.arrange()제가 가진 정확한 문제를 설명하는 사람을 찾았을 때부터 링크에서 문제 설명을 인용했습니다.
사용할 때ggsave()끝나고grid.arrange(),예.
grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) ggsave("sgcirNIR.jpg")
그리드 그림이 아닌 마지막 개별 gg 그림을 저장합니다.표시된 대로 실제로 플롯을 저장할 수 있는 방법이 있습니까?grid.arrange()사용.ggsave()아니면 비슷한 것?이전 방식을 사용하는 것 외에는
jpeg("sgcirNIR.jpg") grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) dev.off()
동일한 링크가 아래 솔루션을 제공합니다.
require(grid)
require(gridExtra)
p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device
ggsave("saving.pdf", p) # need to specify what to save explicitly
하지만, 어떻게 사용해야 할지 모르겠어요.ggsave()의 출력을 저장하기 위해grid.arrange()링크에서 가져온 다음 코드를 호출합니다.
library(ggplot2)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
legend <- g_legend(p1)
lwidth <- sum(legend$width)
## using grid.arrange for convenience
## could also manually push viewports
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"),
main ="this is a title",
left = "This is my global Y-axis title"), legend,
widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
# What code to put here to save output of grid.arrange()?
grid.arrange장치에 직접 그립니다. arrangeGrob반면에, 아무것도 그리지 않고 그로브만 반환합니다.g에 전달할 수 있습니다.ggsave(file="whatever.pdf", g).
ggplot2가 지정되지 않은 경우 기본적으로 마지막 플롯이 저장되는 ggplot 객체와 다르게 작동하는 이유는 ggplot2가 보이지 않게 최신 플롯을 추적하기 때문이며, 저는 그렇게 생각하지 않습니다.grid.arrange이 카운터를 패키지 전용으로 사용해야 합니다.
나는 밥티스트의 제안에 약간의 문제가 있었지만, 마침내 그것을 얻었습니다.사용해야 할 항목은 다음과 같습니다.
# draw your plots
plot1 <- ggplot(...) # this specifies your first plot
plot2 <- ggplot(...) # this specifies your second plot
plot3 <- ggplot(...) # this specifies your third plot
#merge all three plots within one grid (and visualize this)
grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid
#save
g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
ggsave(file="whatever.pdf", g) #saves g
이것은 잘 될 것입니다.
grid.arrange를 pdf 파일에 저장하는 또 다른 쉬운 방법은 pdf():
pdf("filename.pdf", width = 8, height = 12) # Open a new pdf file
grid.arrange(plot1, plot2, plot3, nrow=3) # Write the grid.arrange in the file
dev.off() # Close the file
ggplot 외에 테이블과 같은 다른 것들을 배열에서 병합할 수 있습니다.
arrangeGrob를 사용할 필요가 없으며 grid.arrange의 결과를 플롯에 직접 할당하고 ggsave를 사용하여 저장할 수 있습니다.
p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)
저는 이것에 추가할 가치가 있다고 생각했습니다.위에서 ggsave 오류가 발생하는 문제가 있었습니다. "plot is ggplot2 plot"
이 답변 덕분에: ggplot_build 및 ggplot_gtable을 사용한 후 ggsave로 그래프를 저장하는 것은 위의 코드에 대한 수정 사항이 있습니다.
# draw your plots
plot1 <- ggplot(...) # this specifies your first plot
plot2 <- ggplot(...) # this specifies your second plot
plot3 <- ggplot(...) # this specifies your third plot
#merge all three plots within one grid (and visualize this)
grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid
#save
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
위 라인은 오류를 수정하는 데 필요합니다.
g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
ggsave(file="whatever.pdf", g) #saves g
이제는 저한테 잘 맞습니다.
이것을 먹어보세요.
ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)
간단한 또 다른 솔루션: 바로 다음과 같습니다.grid.arrange()
grid.arrange(plot1, plot2, plot3, nrow=3)
당신은.dev.copy()
dev.copy(pdf,"whatever.pdf")
dev.off()
언급URL : https://stackoverflow.com/questions/17059099/saving-grid-arrange-plot-to-file
'programing' 카테고리의 다른 글
| UITableViewCell의 기본 높이는 얼마입니까? (0) | 2023.07.09 |
|---|---|
| 미발견(약속)유형 오류: vuex에서 null('시작' 읽기) vue 3의 속성을 읽을 수 없습니다. (0) | 2023.07.09 |
| 애플리케이션 대 세션 대 캐시 (0) | 2023.07.09 |
| OpenXML C#을 사용하여 Excel에서 셀의 열 인덱스 가져오기 (0) | 2023.07.09 |
| 루비에서 YAML 파일을 구문 분석하려면 어떻게 해야 합니까? (0) | 2023.07.09 |