【问题标题】:How to add logo on ggplot2 footer如何在 ggplot2 页脚上添加徽标
【发布时间】:2017-01-10 17:27:00
【问题描述】:

如何在 ggplot2 的绘图区域之外添加图像徽标。尝试了 'grid' 包中的 rasterGrob 函数,但这将图像保留在绘图区域内。

这里是启动脚本:

library(ggplot2)
library(png)
library(gridExtra)
library(grid)

gg <- ggplot(df1, aes(x = mpg, y = wt)) + 
       theme_minimal() +
        geom_count() + 
        labs(title = "Title Goes Here", x = "", y = "")

img <- readPNG("fig/logo.png")

这是我正在寻找的结果。

我可以在右侧添加注释,但左侧的徽标是我受到挑战的地方。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用annotation_custom 添加元素,但您需要关闭剪辑,以便图像在绘图区域之外时显示。为了使其可重现,我对您的示例稍作更改。

library(ggplot2)
library(png)
library(gridExtra)
library(grid)

gg <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  theme_minimal() +
  geom_count() + 
  labs(title = "Title Goes Here", x = "", y = "")

img = readPNG(system.file("img", "Rlogo.png", package="png"))

gg = gg + 
  annotation_custom(rasterGrob(img), 
                    xmin=0.95*min(mtcars$mpg)-1, xmax=0.95*min(mtcars$mpg)+1, 
                    ymin=0.62*min(mtcars$wt)-0.5, ymax=0.62*min(mtcars$wt)+0.5) +
  annotation_custom(textGrob("Footer goes here", gp=gpar(col="blue")), 
                    xmin=max(mtcars$mpg), xmax=max(mtcars$mpg), 
                    ymin=0.6*min(mtcars$wt), ymax=0.6*min(mtcars$wt)) +
  theme(plot.margin=margin(5,5,30,5))

# Turn off clipping
gt <- ggplot_gtable(ggplot_build(gg))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

另一种选择是使用ggplot的caption功能添加文本页脚,这样可以节省一些代码:

gg = gg + 
  annotation_custom(rasterGrob(img), 
                    xmin=0.95*min(mtcars$mpg)-1, xmax=0.95*min(mtcars$mpg)+1, 
                    ymin=0.62*min(mtcars$wt)-0.5, ymax=0.62*min(mtcars$wt)+0.5) +
  labs(caption="Footer goes here") +
  theme(plot.margin=margin(5,5,15,5),
        plot.caption=element_text(colour="blue", hjust=1.05, size=15)) 

# Turn off clipping
gt <- ggplot_gtable(ggplot_build(gg))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

【讨论】:

  • 感谢 eipi10,这行得通!但我不确定我是否理解“剪辑”行gt$layout$clip[gt$layout$name=="panel"] &lt;- "off" - 你能扩展一下吗?或任何参考阅读?
  • 当您尝试绘制超出绘图区域的内容时,仅显示绘图区域内的部分。默认情况下,剩余部分是“剪辑的”,意思是不出现。在运行gt &lt;- ggplot_gtable(ggplot_build(gg)) 之后,在控制台中输入gt$layout$clip。输出将显示图中的哪些 grobs(图形对象)已打开剪辑。 ggplot 对象(和其他网格图形,例如lattice 绘图)由“grobs”组成。
  • 至于参考:您可以查看 Paul Murrell 的书“R Graphics, 2nd ed”。或在网络上搜索有关在 R 中使用 grid 图形的文章。还可以在 Stack Overflow 中搜索术语 ggplot、grob 和 grid 的答案。
  • 也许更简单的关闭剪辑方法是在原始 ggplot 中添加 coord_cartesian(clip = "off") 行。
  • 是的,在我回答这个问题时,这不是一个选项。我相信它是在 ggplot2 v3.0.0 中添加的。
【解决方案2】:

从很棒的包 Magick 中添加一个更新的方法:

library(ggplot2)
library(magick)
library(here) # For making the script run without a wd
library(magrittr) # For piping the logo

# Make a simple plot and save it
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() + 
  ggtitle("Cars") +
  ggsave(filename = paste0(here("/"), last_plot()$labels$title, ".png"),
         width = 5, height = 4, dpi = 300)

# Call back the plot
plot <- image_read(paste0(here("/"), "Cars.png"))
# And bring in a logo
logo_raw <- image_read("http://hexb.in/hexagons/ggplot2.png") 

# Scale down the logo and give it a border and annotation
# This is the cool part because you can do a lot to the image/logo before adding it
logo <- logo_raw %>%
  image_scale("100") %>% 
  image_background("grey", flatten = TRUE) %>%
  image_border("grey", "600x10") %>%
  image_annotate("Powered By R", color = "white", size = 30, 
                 location = "+10+50", gravity = "northeast")

# Stack them on top of each other
final_plot <- image_append(image_scale(c(plot, logo), "500"), stack = TRUE)
# And overwrite the plot without a logo
image_write(final_plot, paste0(here("/"), last_plot()$labels$title, ".png"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多