如何在ggplot2页脚上添加徽标 [英] How to add logo on ggplot2 footer

查看:147
本文介绍了如何在ggplot2页脚上添加徽标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ggplot2的绘图区域之外添加图片徽标。从'网格'包中尝试了rasterGrob函数,但它保留了绘图区域内的图像。



下面是启动脚本:

  library(ggplot2)
library(png)
library(gridExtra)
library(grid)
$ b gg < - ggplot(df1,aes(x = mpg,y = wt)) +
theme_minimal()+
geom_count()+
labs(title =Title Goes Here,x =,y =)

img < - readPNG(fig / logo.png)

以下是我正在寻找的结果。





我可以在右侧添加注释但是左边的logo是我遇到的问题。 c> 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))

#关闭剪辑
gt< - ggplot_gtable(ggplot_build(gg))
gt $
grid.draw(gt)

/ code>功能来添加文本页脚,从而节省了一些代码:

  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(color =blue,hjust = 1.05,size = 15))

#关闭剪切
gt< - ggplot_gtable (ggplot_build(gg))
gt $ layout $ clip [gt $ layout $ name ==panel]< - off
grid.draw(gt)

a>


How to add image logo outside the plotting areas for ggplot2. Tried rasterGrob function from 'grid' package, but that keep's the image inside plot area.

Here is the starter script:

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

Here is the outcome I am looking for.

I can add the annotation on the right side, but the logo on the left is where I am getting challenged.

解决方案

You can add the elements with annotation_custom but you need to turn off clipping for the images to show up when they're outside the plot area. I've changed your example slightly in order to make it reproducible.

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)

Another option is to use ggplot's caption feature to add the text footer, which saves some code:

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)

这篇关于如何在ggplot2页脚上添加徽标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆