【问题标题】:Adding a logo to Multi plot output in R or ggplot2在 R 或 ggplot2 中向多图输出添加徽标
【发布时间】:2015-04-01 00:15:08
【问题描述】:

我试图将徽标添加到从 grid.arrange 或arrangeGrob 派生的输出中。

我有以下代码:

库(ggplot2)

p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
         geom_line() +
         ggtitle("Growth curve for individual chicks")

 p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
          geom_point(alpha=.3) +
           geom_smooth(alpha=.2, size=1) +
          ggtitle("Fitted growth curve per diet")

 p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet))         
          + geom_density() +
          ggtitle("Final weight, by diet")

 p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
         geom_histogram(colour="black", binwidth=50) +
         ggtitle("Final weight, by diet") 

我使用 grid.arrange(p1,p2,p3,p4,ncol=2,clip=4) 将多个图放在一个图上。

但我在将徽标插入到上述 grid.arrange 输出时遇到问题。

我尝试了以下方法,但收到以下错误消息。

    b <- rasterGrob(img, width=unit(5,"cm"), x = unit(40,"cm")) 
     z1 <- ggplotGrob(grid.arrange(p1,p2,p3,p4,ncol=2,clip=4)) 
     z1<- gtable_add_grob(z1,b, t=1,l=1, r=5)
     grid.newpage()
      grid.draw(z1)

错误:绘图中没有图层

有没有办法在arrangeGrob或grid.arrange之后在输出中添加logo。

【问题讨论】:

    标签: r


    【解决方案1】:

    不是gtable 的答案,但这是添加徽标的稍微不同的方式,可能会有所帮助

    library(ggplot2)
    library(grid)
    library(png)
    library(gridExtra)
    
    # Read png
    img <- readPNG(system.file("img", "Rlogo.png", package="png"), FALSE)
    
    # Create grobs to add to plot
    my_g <- grobTree(rectGrob(gp=gpar(fill="black")),
                     textGrob("Some text", x=0, hjust=0, gp=gpar(col="white")),
                     rasterGrob(img, x=1, hjust=1))
    
    # Plot
    p <- ggplot(mtcars , aes(wt , mpg)) + 
               geom_line() +
               theme(plot.margin=unit(c(1, 1, 1,1), "cm"))
    
    # Add as a strip along top
    grid.arrange(my_g, arrangeGrob(p,p,p,p, ncol=2), heights=c(1, 9))
    

    【讨论】:

    • @user20650,非常感谢。这就是我一直在寻找的。是否可以将徽标带入情节区域内。
    • 如果要将其添加到绘图中,可以使用 annotation_custom 并指定坐标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2021-01-14
    • 2021-12-19
    • 2020-06-25
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多