从ggrepel获取标签位置的坐标 [英] Getting coordinates for the label locations from ggrepel
问题描述
以下是使用ggplot2和ggrepel进行标签位置优化"的示例:
x = c(0.8846,1.1554,0.9317,0.9703,0.9053,0.9454,1.0146,0.9012,0.9055、1.3307)y = c(0.9828,1.0329,0.931,1.3794,0.9273,0.9605,1.0259,0.9542,0.9717、0.9357)ShortSci = c("MotAlb","PruMod","EriRub","LusMeg","PhoOch","PhoPho","SaxRub","TurMer","TurPil","TurPhi")df <-data.frame(x = x,y = y,z = ShortSci)库(ggplot2)图书馆(ggrepel)ggplot(数据= df,aes(x = x,y = y))+ theme_bw()+geom_text_repel(aes(label = z),box.padding = unit(0.45,"lines"))+geom_point(颜色=绿色",大小= 3)
问题:是否有可能以某种方式将标签位置的坐标作为矢量?
非常感谢!
在@Henrik建议的链接上,有许多有趣的想法可用来获取 ggrepel
标签的位置.
这是个人返工:
x = c(0.8846,1.1554,0.9317,0.9703,0.9053,0.9454,1.0146,0.9012,0.9055,1.3307)y = c(0.9828,1.0329,0.931,1.3794,0.9273,0.9605,1.0259,0.9542,0.9717,0.9357)ShortSci = c("MotAlb","PruMod","EriRub","LusMeg","PhoOch","PhoPho","SaxRub","TurMer","TurPil","TurPhi")df <-data.frame(x = x,y = y,z = ShortSci)库(ggplot2)图书馆(ggrepel)p1<-ggplot(数据= df,aes(x = x,y = y))+ theme_bw()+geom_text_repel(aes(label = z),box.padding = unit(0.45,"lines"))+geom_point(颜色=绿色",大小= 3)x11(宽度= 7,高度= 7)1#获取x和y的绘图范围xrg<-ggplot_build(p1)$ layout $ panel_ranges [[1]] $ x.rangeyrg<-ggplot_build(p1)$ layout $ panel_ranges [[1]] $ y.range图书馆(网格)grid.force()孩子<-childNames(grid.get("textrepeltree",grep = TRUE))#功能:获取单个ggrepel标签的x和y位置get.xy.pos.labs<-function(n){grb<-grid.get(n)data.frame(x = xrg [1] + diff(xrg)* convertX(grb $ x,"native",valueOnly = TRUE),y = yrg [1] + diff(yrg)* convertY(grb $ y,"native",valueOnly = TRUE))}#获取所有ggrepel标签的位置dts<-do.call(rbind,lapply(kids,get.xy.pos.labs))dts $ lab<-df $ z打印(DTS)#x y实验室#1 0.8871681 0.9945523 MotAlb#2 1.1441033 1.0214568 PruMod#3 0.9431081 0.9194218 EriRub#4 0.9977394 1.3906067 LusMeg#5 0.8900197 0.9160821照片#6 0.9597605 0.9471191 PhoPho#7 1.0033184 1.0144658 SaxRub#8 0.8898377 0.9424059 TurMer#9 0.9340703 0.9722663 TurPil#10 1.3063754 0.9358787 TurPhi#使用通过get.xy.pos.labs计算出的位置来绘制标签p2<-ggplot(数据= df,aes(x = x,y = y))+ theme_bw()+geom_text(aes(x = x,y = y,label = lab),data = dts,col ="red")+geom_point(颜色=绿色",大小= 3)x11(宽度= 7,高度= 7)2
更新2018年12月
对于 3.1.0 (及更高版本)的 ggplot2
版本,您需要使用以下代码来获取x和y范围(请参阅@VilmantasGegzna的答案
Below is an example of "label location optimization" using ggplot2 and ggrepel:
x = c(0.8846, 1.1554, 0.9317, 0.9703, 0.9053, 0.9454, 1.0146, 0.9012,
0.9055, 1.3307)
y = c(0.9828, 1.0329, 0.931, 1.3794, 0.9273, 0.9605, 1.0259, 0.9542,
0.9717, 0.9357)
ShortSci = c("MotAlb", "PruMod", "EriRub", "LusMeg", "PhoOch", "PhoPho",
"SaxRub", "TurMer", "TurPil", "TurPhi")
df <- data.frame(x = x, y = y, z = ShortSci)
library(ggplot2)
library(ggrepel)
ggplot(data = df, aes(x = x, y = y)) + theme_bw() +
geom_text_repel(aes(label = z),
box.padding = unit(0.45, "lines")) +
geom_point(colour = "green", size = 3)
Question: is it at all possible to somehow grab the coordinates for the label locations as vectors?
Thank you very much!
At the link suggested by @Henrik there many interesting ideas for getting ggrepel
label locations.
Here is a personal reworking:
x = c(0.8846, 1.1554, 0.9317, 0.9703, 0.9053, 0.9454, 1.0146, 0.9012, 0.9055, 1.3307)
y = c(0.9828, 1.0329, 0.931, 1.3794, 0.9273, 0.9605, 1.0259, 0.9542, 0.9717, 0.9357)
ShortSci = c("MotAlb", "PruMod", "EriRub", "LusMeg", "PhoOch", "PhoPho",
"SaxRub", "TurMer", "TurPil", "TurPhi")
df <- data.frame(x = x, y = y, z = ShortSci)
library(ggplot2)
library(ggrepel)
p1 <- ggplot(data = df, aes(x = x, y = y)) + theme_bw() +
geom_text_repel(aes(label = z),
box.padding = unit(0.45, "lines")) +
geom_point(colour = "green", size = 3)
x11(width=7,height=7)
p1
# Get x and y plot ranges
xrg <- ggplot_build(p1)$layout$panel_ranges[[1]]$x.range
yrg <- ggplot_build(p1)$layout$panel_ranges[[1]]$y.range
library(grid)
grid.force()
kids <- childNames(grid.get("textrepeltree", grep = TRUE))
# Function: get the x and y positions of a single ggrepel label
get.xy.pos.labs <- function(n) {
grb <- grid.get(n)
data.frame(
x = xrg[1]+diff(xrg)*convertX(grb$x, "native", valueOnly = TRUE),
y = yrg[1]+diff(yrg)*convertY(grb$y, "native", valueOnly = TRUE)
)
}
# Get positions of all ggrepel labels
dts <- do.call(rbind, lapply(kids, get.xy.pos.labs))
dts$lab <- df$z
print(dts)
# x y lab
# 1 0.8871681 0.9945523 MotAlb
# 2 1.1441033 1.0214568 PruMod
# 3 0.9431081 0.9194218 EriRub
# 4 0.9977394 1.3906067 LusMeg
# 5 0.8900197 0.9160821 PhoOch
# 6 0.9597605 0.9471191 PhoPho
# 7 1.0033184 1.0144658 SaxRub
# 8 0.8898377 0.9424059 TurMer
# 9 0.9340703 0.9722663 TurPil
# 10 1.3063754 0.9358787 TurPhi
# Plot labels using the positions calculated using get.xy.pos.labs
p2 <- ggplot(data = df, aes(x = x, y = y)) + theme_bw() +
geom_text(aes(x=x,y=y, label=lab), data=dts, col="red")+
geom_point(colour = "green", size = 3)
x11(width=7,height=7)
p2
UPDATE December 2018
With ggplot2
version 3.1.0 (and newer) you need to use the following code to get the x- and y-range (see the answer of @VilmantasGegzna here for details):
# Get x and y plot ranges
ggp1 <- ggplot_build(p1)
xrg <- ggp1$layout$panel_params[[1]]$x.range
yrg <- ggp1$layout$panel_params[[1]]$y.range
Here are the plot with labels given by geom_text_repel
and the plot with the labels positioned using the calculated positions.
这篇关于从ggrepel获取标签位置的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!