我想知道是否可以在R中找到标签的坐标(即x,y)(如下图蓝色圆圈所示)dotchart?
y = rnorm(20)
groups = factor( rep(1:2, times = c(5, 15) ) )
dotchart(y, groups = groups)
Run Code Online (Sandbox Code Playgroud)
更新:我也在问y下图中的双向箭头之间的点的坐标是什么(假设我知道x该点,但是我希望该点位于双向箭头之间,y那么我可以将点放在双箭头之间的区域):
查看dotchart函数内部,可以看到使用mtext以下命令编写了组标签:
mtext(glabels, side = 2, line = goffset, at = gpos, adj = 0,
col = "red", las = 2, cex = 1)
Run Code Online (Sandbox Code Playgroud)
gpos组标签位置的向量在哪里,计算公式为:
gpos <- rev(cumsum(rev(tapply(groups, groups, length)) + 2) - 1)
#########
1 2
23 16
Run Code Online (Sandbox Code Playgroud)
下面我们尝试在与组标签打印位置完全相同的位置(和红色)打印组标签dotcharts:
graphics.off()
set.seed(1)
y = rnorm(20)
groups = factor( rep(1:2, times = c(5, 15) ) )
dotchart(y, groups = groups)
glabels <- levels(groups)
linch <- 0
ginch <- max(strwidth(glabels, "inch"), na.rm = TRUE)
goffset <- 0.4
nmai <- par("mai")
nmai[2L] <- nmai[4L] + max(linch + goffset, ginch) + 0.1
par(mai = nmai)
lheight <- par("csi")
gpos <- rev(cumsum(rev(tapply(groups, groups, length)) + 2) - 1)
ginch <- max(strwidth(glabels, "inch"), na.rm = TRUE)
goffset <- (max(linch + 0.2, ginch, na.rm = TRUE) + 0.1)/lheight
mtext(glabels, side = 2, line = goffset, at = gpos, adj = 0,
col = "red", las = 2, cex = 1)
Run Code Online (Sandbox Code Playgroud)
编辑。
从此链接下载的修改版本,dotchart并将其保存在您的工作目录中mydotchart.r
然后输入以下代码:
source("mydotchart.r")
set.seed(1)
y = rnorm(20)
groups = factor( rep(1:2, times = c(5, 15) ) )
mydotchart(y, groups = groups)
Run Code Online (Sandbox Code Playgroud)
该函数mydotchart.r提供以下输出:
$gpos
1 2
23 16
$linepos
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 19 20 21 22
Run Code Online (Sandbox Code Playgroud)
其中,gpos是组标签linepos的y位置,是水平虚线灰色线y位置的向量。
使用linepos它可以计算以上问题中双箭头之间的位置。
希望它能对您有所帮助。