2017-12-17 25 views
2

Rが新しく、同じページにRを表示しようとしました library(ggpubr)ggarrange()関数を使用しようとしました。グラフと画像を同じパネルに表示するにはどうすればいいですか?

イメージをインポートするには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

これは素晴らしい動作です!ありがとうございました –

関連する問題