使用annotation_custom在ggplot上以编程方式定位图像。

huangapple go评论65阅读模式
英文:

Programmatically position image on ggplot using annotation_custom

问题

我正在尝试创建一个函数(在R包内部),该函数将图像添加到ggplot的右下角。由于这将包含在R包中,我希望这个函数可以在不需要用户干预的情况下,以编程方式将图像定位到绘图的右下角。

这是我目前的进展:

# 设置
library(tidyverse)

get_png <- function(filename) {
  grid::rasterGrob(png::readPNG(filename), interpolate = TRUE)
}

# DaSL徽标
l <- get_png("logo.png")

# 主要函数
add_dasl_logo <- function(logo) {
  list(annotation_custom(logo, xmin = 5, xmax = 7.5, ymin = 0, ymax = 15), 
       coord_cartesian(clip = "off"),
       theme(plot.margin = unit(c(1, 1, 3, 1), "lines"))
  )
}

# 绘图
ggplot(mpg, aes(displ, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

图像文件(logo.png):使用annotation_custom在ggplot上以编程方式定位图像。

目前,x-y min/max 位置是手动设置的,针对特定的ggplot。我不希望普通的ggplot2用户去计算xminxmaxyminymax坐标。

如何在add_dasl_logo()函数中以编程方式找到这些坐标并使用它们,而不需要用户干预?

英文:

I am trying to create a function (inside an R package) that adds an image to the lower right corner of a ggplot. Since this will be included in a R package, I want this function to programmatically position this image to the bottom right of the plot, without any user intervention.

Here is what I have so far:

# Setup
library(tidyverse)

get_png &lt;- function(filename) {
  grid::rasterGrob(png::readPNG(filename), interpolate = TRUE)
}

# DaSL logo
l &lt;- get_png(&quot;logo.png&quot;)

# Main function
add_dasl_logo &lt;- function(logo) {
  list(annotation_custom(logo, xmin = 5, xmax = 7.5, ymin = 0, ymax = 15), 
       coord_cartesian(clip = &quot;off&quot;),
       theme(plot.margin = unit(c(1, 1, 3, 1), &quot;lines&quot;))
  )
}

# Plot
ggplot(mpg, aes(displ, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

Image file (logo.png): 使用annotation_custom在ggplot上以编程方式定位图像。

Right now, the x-y min/max locations are manually set for this specific ggplot. I don't expect the average ggplot2 user to figure out the xmin, xmax, ymin, ymax coordinates.

How do I programmatically find these coordinates and use them in add_dasl_logo() without any user intervention?

答案1

得分: 3

不要别的内容,以下是翻译好的部分:

"Instead of setting the position for your logo in data coordinates via annotation_custom, you could set the position in relative coordinates directly in grid::rasterGrob. This way the logo will be placed at the same position and with the same height and width for each plot."

# 设置
library(tidyverse)

get_png <- function(filename) {
  grid::rasterGrob(png::readPNG(filename),
    interpolate = TRUE,
    x = unit(1, "npc") + unit(10, "pt"),
    y = unit(0, "npc") - unit(20, "pt"),
    height = unit(10, "pt"),
    hjust = 1,
    vjust = 1
  )
}

download.file("https://i.stack.imgur.com/uj2rP.png", "logo.png")

# DaSL logo
l <- get_png("logo.png")

# 主要功能
add_dasl_logo <- function(logo) {
  list(
    annotation_custom(logo),
    coord_cartesian(clip = "off"),
    theme(plot.margin = unit(c(1, 1, 3, 1), "lines"))
  )
}

ggplot(mpg, aes(displ, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

使用annotation_custom在ggplot上以编程方式定位图像。

ggplot(mpg, aes(cty, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

使用annotation_custom在ggplot上以编程方式定位图像。

英文:

Instead of setting the position for your logo in data coordinates via annotation_custom you could set the position in relative coordinates directly in grid::rasterGrob. This way the logo will be placed at the same position and with the same height and width for each plot.

# Setup
library(tidyverse)

get_png &lt;- function(filename) {
  grid::rasterGrob(png::readPNG(filename),
    interpolate = TRUE,
    x = unit(1, &quot;npc&quot;) + unit(10, &quot;pt&quot;),
    y = unit(0, &quot;npc&quot;) - unit(20, &quot;pt&quot;),
    height = unit(10, &quot;pt&quot;),
    hjust = 1,
    vjust = 1
  )
}

download.file(&quot;https://i.stack.imgur.com/uj2rP.png&quot;, &quot;logo.png&quot;)

# DaSL logo
l &lt;- get_png(&quot;logo.png&quot;)

# Main function
add_dasl_logo &lt;- function(logo) {
  list(
    annotation_custom(logo),
    coord_cartesian(clip = &quot;off&quot;),
    theme(plot.margin = unit(c(1, 1, 3, 1), &quot;lines&quot;))
  )
}

ggplot(mpg, aes(displ, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

使用annotation_custom在ggplot上以编程方式定位图像。<!-- -->

ggplot(mpg, aes(cty, hwy, colour = hwy)) +
  geom_point() +
  add_dasl_logo(l)

使用annotation_custom在ggplot上以编程方式定位图像。<!-- -->

huangapple
  • 本文由 发表于 2023年5月29日 12:35:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定