是否可以在ggplot图表中添加图像,而该图像将位于图表区域之外?
我知道可以使用annotate_raster和annotate_custom将图像添加到图表,但是它们都在图表内部添加图像。我需要在图表上方的右上角添加公司徽标-在标题级别。
使用annotate_custom添加图像的示例:
Inserting an image to 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")
最佳答案
刻面时,annotation_custom
在所有面板中绘制注释。因此,annotation-custom
可能不是最佳方法。这是使用grid
包中的函数的两次尝试。两者都不是完全自动的,但是您可能可以调整其中一个以满足您的需求。他们建立了一个2 X 2的网格,使用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)
标题与徽标不完全一致。一种解决方法是从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)