ggplot2中的角标签 [英] Corner Labels in ggplot2

查看:100
本文介绍了ggplot2中的角标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣尝试为要在ggplot中准备的多面板图形创建简单的边角标签. 这类似于先前提出的问题,但是答案仅说明了如何在图的顶部添加标签,而不是按照许多期刊所需的格式生成边角标签.我希望复制类似于ggplot2plotrix函数corner.label()的内容.

I'm interested in trying to create simple corner labels for a multipanel figure I am preparing in ggplot. This is similar to this previously asked question, but the answers only explained how to include a label at the top of the plot, not produce a corner label in the format required by many journals. I hope to replicate something similar to the plotrix function corner.label() in ggplot2.

这是一个使用plottrix的示例,该示例是我想在ggplot2中重新创建的内容.

Here is an example using plottrix of what I would like to recreate in ggplot2.

require(plotrix)

foo1<-rnorm(50,25,5)
foo2<-rpois(50,25)
foo3<-rbinom(50,25,0.5)
foo4<-rnbinom(50,25,0.5)

par(mfrow=c(2,2))
hist(foo1)
corner.label(label='a',figcorner=T)
hist(foo2)
corner.label(label='b',figcorner=T)
hist(foo3)
corner.label(label='c',figcorner=T)
hist(foo4)
corner.label(label='d',figcorner=T)

这将产生以下结果:

感谢您的任何帮助!

推荐答案

我遇到了同样的问题,并提出了以下解决方案,该解决方案略有不同:

I had the same problem and came up with the following solution, which is a bit different:

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

示例数据

a <- 1:20
b <- sample(a, 20)
c <- sample(b, 20)
d <- sample(c, 20)

创建数据框

mydata   <- data.frame(a, b, c, d)

创建示例图

myplot1  <- ggplot(mydata, aes(x=a, y=b)) + geom_point()
myplot2  <- ggplot(mydata, aes(x=b, y=c)) + geom_point()
myplot3  <- ggplot(mydata, aes(x=c, y=d)) + geom_point()
myplot4  <- ggplot(mydata, aes(x=d, y=a)) + geom_point()

设置边角标签

myplot1 <- arrangeGrob(myplot1, top = textGrob("A", x = unit(0, "npc")
         , y   = unit(1, "npc"), just=c("left","top"),
         gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

myplot2 <- arrangeGrob(myplot2, top = textGrob("B", x = unit(0, "npc")
         , y = unit(1, "npc"), just=c("left","top"),
         gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

myplot3 <- arrangeGrob(myplot3, top = textGrob("C", x = unit(0, "npc")
        , y  = unit(1, "npc"), just=c("left","top"),
        gp=gpar(col="black", fontsize=18, fontfamily="Times Roman")))

myplot4 <- arrangeGrob(myplot4, top = textGrob("D", x = unit(0, "npc")
        , y = unit(1, "npc"), just=c("left","top"),
        gp=gpar(col="black",    fontsize=18, fontfamily="Times Roman")))

在一页上绘制所有图

grid.arrange(myplot1, myplot2, myplot3, myplot4, ncol = 2)

这篇关于ggplot2中的角标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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