11

チャート領域の外側にあるggplotチャートに画像を追加することは可能ですか?

annotate_rasterとannotate_customを使用してグラフに画像を追加できることは知っていますが、どちらもグラフ内に画像を追加します。チャートの右上隅に、タイトルレベルで会社のロゴを追加する必要があります。

annotate_customを使用して画像を追加する例: ggplot2に画像を挿入する

更新:user1317221_Gからの推奨事項に従って、画像を外部に配置することができます。

library(png)
library(grid)
library(ggplot2)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

g <- rasterGrob(img, interpolate=TRUE)
plt <- qplot(1:10, 1:10, geom="blank") +
      opts(title = 'Title') + 
      geom_point() 
plt2 <- plt + annotation_custom(g, xmin=9, xmax=10, ymin=10.5, ymax=11.25)

gt <- ggplot_gtable(ggplot_build(plt2))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

画像を自動的に配置したい-xmin/xmaxとymin/ymaxを自動的に決定します。従来のグラフィックを使用して、par('usr')を呼び出してグラフのパラメーターを取得できますが、これはggplotグラフには適用されません。ggplotでプロット領域を決定する簡単な方法はありますか?たとえば、pltのサイズを取得し、制限の値をplt2にプラグインします

UPDATE2:ファセットを使用する場合、この方法も実際には機能しません。たとえば、次のグラフのタイトルレベルの右隅にロゴを配置したいのですが、上記の方法を使用するとエラーが発生します。

d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) + 
     xlim(0, 2) + 
     stat_binhex(na.rm = TRUE) + 
     labs(title = 'Title') +
     theme(aspect.ratio = 1) + 
     facet_wrap(~ color, scales = "free_x")
4

1 に答える 1

7

ファセット時annotation_customに、すべてのパネルに注釈を描画します。したがって、annotation-custom最善の方法ではない可能性があります。gridパッケージの関数を使用した2つの試みを次に示します。どちらも完全に自動化されているわけではありませんが、ニーズに合わせてどちらか一方を適応させることができる場合があります。コマンドを使用して表示される2X2グリッドを設定しgrid.show.layout()ます。最初の例では、ファセットプロットがパネル全体に表示され、右上のビューポートにロゴが含まれています。たまたま、あなたのプロットには、ロゴのための明確なスペースがあります。レイアウト内のビューポートが占める行と列をlayout.pos.rowどのように指定するかに注意してください。layout.pos.col

library(ggplot2)
library(png)
library(grid)

# Get the logo
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img)

# Set the size of the viewport to contain the logo
size = unit(2, "cm")

# Get the graph
d <- ggplot(diamonds, aes(carat, price)) + 
     xlim(0, 2) + 
     stat_binhex(na.rm = TRUE) + 
     labs(title = 'Title') +
     theme(aspect.ratio = 1) + 
     facet_wrap(~ color, scales = "free_x")

# Set up the layout for grid 
heights = unit.c(size, unit(1, "npc") - size)
widths = unit.c(unit(1, "npc") - size, size)
lo = grid.layout(2, 2, widths = widths, heights = heights)
# Show the layout
grid.show.layout(lo)

# Position the elements within the viewports
grid.newpage()
pushViewport(viewport(layout = lo))

    # The plot
pushViewport(viewport(layout.pos.row=1:2, layout.pos.col = 1:2))
print(d, newpage=FALSE)
popViewport()

    # The logo
pushViewport(viewport(layout.pos.row=1, layout.pos.col = 2))
print(grid.draw(g), newpage=FALSE)
popViewport()
popViewport()

# To save the object
g = grid.grab()

grid.newpage()
grid.draw(g)

タイトルがロゴと完全に一致しているわけではありません。1つの修正は、ggplotからタイトルを削除し、タイトルを含む別のtextGrobを描画してから、ロゴを含むビューポートの横の左上のビューポートにtextGrobを配置することです。

# Get the logo
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img)

# Set the size of the viewport to contain the logo
size = unit(2, "cm")

# Get the graph
d <- ggplot(diamonds, aes(carat, price)) + 
     xlim(0, 2) + 
     stat_binhex(na.rm = TRUE) + 
     # labs(title = 'Title') +
     theme(aspect.ratio = 1) + 
     facet_wrap(~ color, scales = "free_x")

# and the title
title = textGrob("Title", gp = gpar(face = "bold", cex = 2))

# Set up the layout for grid 
heights = unit.c(size, unit(1, "npc") - size)
widths = unit.c(unit(1, "npc") - 1.5*size, size)
lo = grid.layout(2, 2, widths = widths, heights = heights)
# Show the layout
grid.show.layout(lo)

# Position the elements within the viewports
grid.newpage()
pushViewport(viewport(layout = lo))

    # The plot
pushViewport(viewport(layout.pos.row=2, layout.pos.col = 1:2))
print(d, newpage=FALSE)
popViewport()

    # The logo
pushViewport(viewport(layout.pos.row=1, layout.pos.col = 2))
print(grid.draw(g), newpage=FALSE)
popViewport()

    # The title
pushViewport(viewport(layout.pos.row=1, layout.pos.col = 1))
print(grid.draw(title), newpage=FALSE)
popViewport()
popViewport()

# To save the object
g = grid.grab()

grid.newpage()
grid.draw(g)

ここに画像の説明を入力してください

于 2012-09-18T23:02:45.047 に答える