2017-12-17 6 views
2

R이 처음인데 R을 사용하여 동일한 페이지에 그래프와 이미지를 표시하려고 시도했습니다. library(ggpubr)ggarrange() 함수를 사용해 보았습니다.R과 동일한 패널에 표시 할 그래프와 이미지를 만드는 방법

이미지를 가져 오려면 library(png)readPNG()을 사용하여 이미지를 가져 오십시오.

enter image description here

내가 패널을 만드는 데 사용되는 코드는 다음과 같습니다 :

저주의
library(ggpubr) 
library(png) 

data("ToothGrowth") 

bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", 
       color = "dose", palette = "jco") 
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len", 
       color = "dose", palette = "jco", binwidth = 1) 

img1 <- readPNG("image1.png") 
img2 <- readPNG("image2.png") 

im_A <- ggplot() + background_image(img1) # tried to insert the image as background.. there must be a better way 
im_B <- ggplot() + background_image(img2) 

ggarrange(im_A, im_B, dp, bxp, 
      labels = c("A", "B", "C", "D"), 
      ncol = 2, nrow = 2) 

내가 수동으로 이미지를 삽입

내가 목표로하고있어 최종 결과는이 같은 것입니다 파워 포인트를 사용합니다.

감사합니다.

답변

1

나는 코드가 거의 있다고 생각합니다. theme 함수를 사용하여 여백을 추가하면 다음과 같은 결과를 얻을 수 있습니다. enter image description here

아래 코드를 참조하십시오. 유일한 추가는 두 이미지 모두에 대해 theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm"))입니다.

library(ggpubr) 
library(png) 

data("ToothGrowth") 

bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", 
       color = "dose", palette = "jco") 
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len", 
       color = "dose", palette = "jco", binwidth = 1) 

img1 <- readPNG("~/Personal/Wallpapers/375501.png") 
img2 <- readPNG("~/Personal/Wallpapers/665150.png") 

im_A <- ggplot() + 
    background_image(img1) + 
    # This ensures that the image leaves some space at the edges 
    theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm")) 

im_B <- ggplot() + background_image(img2) + 
    theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm")) 

ggarrange(im_A, im_B, dp, bxp, 
      labels = c("A", "B", "C", "D"), 
      ncol = 2, nrow = 2) 
+1

멋진 작품입니다. 고맙습니다 –