将图像插入图表区域外的ggplot

ily*_*lya 10 r ggplot2

是否可以将图像添加到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)
Run Code Online (Sandbox Code Playgroud)

我想自动放置图像 - 自动确定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")
Run Code Online (Sandbox Code Playgroud)

San*_*att 6

在构面时,annotation_custom在所有面板中绘制注释.因此,annotation-custom可能不是最好的方法.以下是使用grid包中的函数的两次尝试.两者都不是完全自动的,但您可以根据自己的需要调整其中一种.他们设置了一个2 X 2网格,使用该grid.show.layout()命令显示.在第一个中,刻面图填充整个面板,右上角视口包含徽标.恰好在你的情节中,标志有明显的空间.注意如何layout.pos.rowlayout.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)
Run Code Online (Sandbox Code Playgroud)

标题与徽标不完全一致.一个解决方法是从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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 使用像`png`,`pdf`等内置图形设备.使用`?png`获取更多信息.`ggsave`很好,但它不能处理所有情况. (2认同)