par(fig=c(...), new=T)
을 사용할 때 삽입 그래프를 만들 수 있다는 것을 알고 있습니다. 그러나 ggplot2 라이브러리를 사용하여 '삽입 된'그래프를 만들 수 있는지 궁금합니다.삽입 그래프를 만들 수 있습니까?
업데이트 1 : par()
을 ggplot2와 함께 사용해 보았지만 작동하지 않습니다.
업데이트 2 : grid::viewport()
을 사용하여 ggplot2 GoogleGroups에서 해결책을 찾았습니다.
par(fig=c(...), new=T)
을 사용할 때 삽입 그래프를 만들 수 있다는 것을 알고 있습니다. 그러나 ggplot2 라이브러리를 사용하여 '삽입 된'그래프를 만들 수 있는지 궁금합니다.삽입 그래프를 만들 수 있습니까?
업데이트 1 : par()
을 ggplot2와 함께 사용해 보았지만 작동하지 않습니다.
업데이트 2 : grid::viewport()
을 사용하여 ggplot2 GoogleGroups에서 해결책을 찾았습니다.
the book의 8.4 절에서이를 수행하는 방법에 대해 설명합니다. 요령은 grid
패키지의 viewport
을 사용하는 것입니다. 학습 R 블로그에
#Any old plot
a_plot <- ggplot(cars, aes(speed, dist)) + geom_line()
#A viewport taking up a fraction of the plot area
vp <- viewport(width = 0.4, height = 0.4, x = 0.8, y = 0.2)
#Just draw the plot twice
png("test.png")
print(a_plot)
print(a_plot, vp = vp)
dev.off()
나는 ggsave에서 작동하는 솔루션을 선호합니다. 주위에 인터넷 검색을 많이 후 나는 위치와 삽입 플롯 크기 조정에 대한 일반 식이다 (이 함께했다.
library(tidyverse)
plot1 = qplot(1.00*mpg, 1.00*wt, data=mtcars) # Make sure x and y values are floating values in plot 1
plot2 = qplot(hp, cyl, data=mtcars)
plot(plot1)
# Specify position of plot2 (in percentages of plot1)
# This is in the top left and 25% width and 25% height
xleft = 0.05
xright = 0.30
ybottom = 0.70
ytop = 0.95
# Calculate position in plot1 coordinates
# Extract x and y values from plot1
l1 = ggplot_build(plot1)
x1 = l1$layout$panel_ranges[[1]]$x.range[1]
x2 = l1$layout$panel_ranges[[1]]$x.range[2]
y1 = l1$layout$panel_ranges[[1]]$y.range[1]
y2 = l1$layout$panel_ranges[[1]]$y.range[2]
xdif = x2-x1
ydif = y2-y1
xmin = x1 + (xleft*xdif)
xmax = x1 + (xright*xdif)
ymin = y1 + (ybottom*ydif)
ymax = y1 + (ytop*ydif)
# Get plot2 and make grob
g2 = ggplotGrob(plot2)
plot3 = plot1 + annotation_custom(grob = g2, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
plot(plot3)
ggsave(filename = "test.png", plot = plot3)
# Try and make a weird combination of plots
g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)
g3 <- ggplotGrob(plot3)
library(gridExtra)
library(grid)
t1 = arrangeGrob(g1,ncol=1, left = textGrob("A", y = 1, vjust=1, gp=gpar(fontsize=20)))
t2 = arrangeGrob(g2,ncol=1, left = textGrob("B", y = 1, vjust=1, gp=gpar(fontsize=20)))
t3 = arrangeGrob(g3,ncol=1, left = textGrob("C", y = 1, vjust=1, gp=gpar(fontsize=20)))
final = arrangeGrob(t1,t2,t3, layout_matrix = cbind(c(1,2), c(3,3)))
grid.arrange(final)
ggsave(filename = "test2.png", plot = final)
하나가 같은 다른 음모를 원한다면, 지적 가치
필자는 ggplot 패키지를 업데이트했고 이제 plot1 좌표에서 위치를 추출하기 위해 형식이 다음과 같아야합니다 :'l1 $ layout $ panel_ranges [[1]] $ x.range [1]'. 'l1 $ layout $ panel_ranges .... '을 참고하십시오. – anotherFishGuy
당신 말이 맞아요. 그에 따라 대답을 업데이트했습니다. – pallevillesen
인트 엣, 중요한 라인은'print (another_plot, vp = vp)'입니다. +1 – mts