英文:
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)
目前,x-y min/max 位置是手动设置的,针对特定的ggplot。我不希望普通的ggplot2用户去计算xmin
、xmax
、ymin
、ymax
坐标。
如何在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 <- function(filename) {
grid::rasterGrob(png::readPNG(filename), interpolate = TRUE)
}
# DaSL logo
l <- get_png("logo.png")
# Main function
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"))
)
}
# Plot
ggplot(mpg, aes(displ, hwy, colour = hwy)) +
geom_point() +
add_dasl_logo(l)
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)
ggplot(mpg, aes(cty, hwy, colour = hwy)) +
geom_point() +
add_dasl_logo(l)
英文:
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 <- 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")
# Main function
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)
<!-- -->
ggplot(mpg, aes(cty, hwy, colour = hwy)) +
geom_point() +
add_dasl_logo(l)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论