gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:59:17 25 4
gpt4 key购买 nike

是否可以将图像添加到图表区域之外的 ggplot 图表中?

我知道可以使用 annotate_raster 和 annotate_custom 将图像添加到图表中,但是它们都在图表内添加图像。我需要在图表上方的右上角 - 标题级别添加公司 Logo 。

使用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:如果您使用分面,此方法也不起作用。例如,我想将 Logo 放在标题级别的右上角,如下图所示,如果使用上面的方法,则会出现错误。

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() 命令进行显示。在第一个中,多面图填充了整个面板,右上角的视口(viewport)包含 Logo 。碰巧在你的绘图中,有足够的空间放置 Logo 。请注意 layout.pos.rowlayout.pos.col 如何给出布局中视口(viewport)占用的行和列。

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)

标题与 Logo 不完全一致。一种修复方法是从 ggplot 中删除标题,绘制一个包含标题的单独 textGrob,然后将 textGrob 放置在包含 Logo 的视口(viewport)旁边的左上角视口(viewport)中。

# 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)

enter image description here

关于r - 将图像插入图表区域外的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12463691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com