R 在每个角上放置绘图文本

R 在每个角上放置绘图文本,r,ggplot2,R,Ggplot2,我有一个散点图,有一条水平线和一条垂直线,它们描述了阈值,因此它们将图分为四个象限。我想标记象限。我认为最好的方法是在图表的四个角上各写一个数字(欢迎其他建议!) 我设法在每个象限的角落放了一个文本,但位置并不完美。我假设问题与轴的缩放不同有关(值的范围大致相同,但我的图形的宽度大约是高度的三倍) 目前我采取以下方式。首先我用点和两条线创建图形,然后我构建它以获得两个轴的范围,我使用它来调整文本的位置 plot.build = ggplot_build(plot) xpos = numeric

我有一个散点图,有一条水平线和一条垂直线,它们描述了阈值,因此它们将图分为四个象限。我想标记象限。我认为最好的方法是在图表的四个角上各写一个数字(欢迎其他建议!)

我设法在每个象限的角落放了一个文本,但位置并不完美。我假设问题与轴的缩放不同有关(值的范围大致相同,但我的图形的宽度大约是高度的三倍)

目前我采取以下方式。首先我用点和两条线创建图形,然后我构建它以获得两个轴的范围,我使用它来调整文本的位置

plot.build = ggplot_build(plot)

xpos = numeric(4)
xpos[2] = xpos[3] = plot.build$panel$ranges[[1]]$x.range[1]
xpos[1] = xpos[4] = plot.build$panel$ranges[[1]]$x.range[2]

ypos = numeric(4)
ypos[1] = ypos[2] = plot.build$panel$ranges[[1]]$y.range[2]
ypos[3] = ypos[4] = plot.build$panel$ranges[[1]]$y.range[1]


plot = plot + geom_text(aes(x2,y2,label = texthere), 
                    data.frame(x2=xpos, y2=ypos, texthere=c("1", "2", "3", "4")),
                    color="#4daf4a", size=4)
基本上这是可行的,但由于缩放,两个轴的数字和绘图边界之间的空间不相同。我试图调整文本的x位置,但ggplot只是扩展了值的范围,位置(相对于边框)保持不变。有没有一种方法可以在不改变值范围的情况下移动文本


提前谢谢

此示例使用
Inf
-Inf
值将文本定位在角落,然后使用geom_文本中的
hjust
vjust
参数将文本定位在绘图内。使用
hjustvar
vjustvar
将它们进一步定位到绘图内或绘图外

正如@baptiste所提到的,最好为注释使用一个新的数据集

df <- data.frame(x2=rnorm(100),y2=rnorm(100));library(ggplot2)

annotations <- data.frame(
        xpos = c(-Inf,-Inf,Inf,Inf),
        ypos =  c(-Inf, Inf,-Inf,Inf),
        annotateText = c("Bottom Left (h0,v0)","Top Left (h0,v1)"
                        ,"Bottom Right h1,v0","Top Right h1,v1"),
        hjustvar = c(0,0,1,1) ,
        vjustvar = c(0,1,0,1)) #<- adjust


  ggplot(df, aes(x2, y2)) + geom_point()+
            geom_text(data=annotations,aes(x=xpos,y=ypos,hjust=hjustvar,vjust=vjustvar,label=annotateText))


希望这能奏效

添加注释时,请确保提供一个新的数据集,或使用annotate,否则多个标签将叠加在一起,呈现锯齿状外观。这是另一个答案的最小变化

df <- data.frame(x2=rnorm(100),y2=rnorm(100))
library(ggplot2)

annotations <- data.frame(
   xpos = c(-Inf,-Inf,Inf,Inf),
   ypos =  c(-Inf, Inf,-Inf,Inf),
   annotateText = c("Text","tExt","teXt","texT"),
   hjustvar = c(0,0,1,1) ,
   vjustvar = c(0,1.0,0,1))


  ggplot(df, aes(x2, y2)) + geom_point()+
  geom_text(data = annotations, aes(x=xpos,y=ypos,hjust=hjustvar,
                vjust=vjustvar,label=annotateText))

df我想我会在给出的答案基础上进行扩展,制作一些看起来更美观的东西。首先,使用hjust和vjust时文本移动的方向可能有点违反直觉(至少对我来说是这样),因此我对每一行都进行了注释,以帮助其他人理解

library(tidyverse)

##Example 1
annotations1 <- data.frame(
  xpos = c(-Inf, -Inf, Inf, Inf), ypos =  c(-Inf, Inf, -Inf, Inf), #left-bottom, left-top, right-bottom, right-top
  annotateText = c("Text", "tExt", "teXt", "texT"),
  # hjustvar = c(0,0,1,1), vjustvar = c(0,1,0,1))   #original placement in each corner
  hjustvar = c(-.5,   #shifts bottom left 'Text' to the right; make more negative to move it further right
               -.5,   #shifts top right 'tExt' to the right; make more negative to move it further right
               1.5,   #shifts bottom right 'teXt' to the left; make more positive to move it further left
               1.5),  #shifts top right 'texT' to the left; make more positive to move it further left
  vjustvar = c(-1,    #shifts bottom left 'Text' upward; make more negative to move it further up
               2,     #shifts top right 'tExt' downward; make more positive to move it further down
               -1,    #shifts bottom right 'teXt' upward; make more negative to move it further up
               2)     #shifts top right 'texT' downward; make more positive to move it further down
)

df1 <- data.frame(x1 = sample(c(-5:5), size = 100, replace = TRUE), y1 = sample(c(-5:5), size = 100, replace = TRUE))

ggplot(df1, aes(x1, y1)) + geom_point() +
  xlim(-6, 6) + ylim(-6, 6) + 
  geom_text(data = annotations1, aes(x = xpos, y = ypos, hjust = hjustvar, vjust = vjustvar, label = annotateText))
库(tidyverse)
##例1

注释1欢迎来到SO!请提供一个,一个数据集,可能还有一张你当前输出的图片,显示错误。我想添加一张图片,但我没有足够的信誉点来完成。谢谢你的回答!不幸的是,它只是部分起作用。上面两个文本已打印,下面两个文本未打印。V6不应该也在0和1之间吗?但我还有一个问题:我想从两个轴上做一个相等(绝对)的缩进。我如何使用hjust和vjust做到这一点?hjust正数=左,hjust负数=右。vjust正极=下降,vjust负极=上升。调整量取决于实际数据变量的比例。所以你必须找到适合的东西。目前我使用这些选项:hjustvar=c(1,0,0,1)vjustvar=c(1,1,0,0)text=c(“1”,“2”,“3”,“4”)1(右上角)和2(左上角)被打印,3和4不被打印。谢谢,这四个数字现在都打印出来了。我只有三个问题:1。为什么在一种情况下对vjustvar使用“1.0”?2.为什么两个较低的文本与底部有一定距离?3.如果我将例如1更改为0.9,则数字不应直接位于拐角处。我只是尝试使用0.9和0.9作为右上角的数字,但没有任何变化。
library(tidyverse)

##Example 1
annotations1 <- data.frame(
  xpos = c(-Inf, -Inf, Inf, Inf), ypos =  c(-Inf, Inf, -Inf, Inf), #left-bottom, left-top, right-bottom, right-top
  annotateText = c("Text", "tExt", "teXt", "texT"),
  # hjustvar = c(0,0,1,1), vjustvar = c(0,1,0,1))   #original placement in each corner
  hjustvar = c(-.5,   #shifts bottom left 'Text' to the right; make more negative to move it further right
               -.5,   #shifts top right 'tExt' to the right; make more negative to move it further right
               1.5,   #shifts bottom right 'teXt' to the left; make more positive to move it further left
               1.5),  #shifts top right 'texT' to the left; make more positive to move it further left
  vjustvar = c(-1,    #shifts bottom left 'Text' upward; make more negative to move it further up
               2,     #shifts top right 'tExt' downward; make more positive to move it further down
               -1,    #shifts bottom right 'teXt' upward; make more negative to move it further up
               2)     #shifts top right 'texT' downward; make more positive to move it further down
)

df1 <- data.frame(x1 = sample(c(-5:5), size = 100, replace = TRUE), y1 = sample(c(-5:5), size = 100, replace = TRUE))

ggplot(df1, aes(x1, y1)) + geom_point() +
  xlim(-6, 6) + ylim(-6, 6) + 
  geom_text(data = annotations1, aes(x = xpos, y = ypos, hjust = hjustvar, vjust = vjustvar, label = annotateText))
##Example 2
annotations2 <- data.frame(
  xpos = c(-Inf, -Inf, Inf, Inf), ypos =  c(-Inf, Inf, -Inf, Inf),
  annotateText = c("Text", "tExt", "teXt", "texT"),
  hjustvar = c(-2, -2, 3, 3),
  vjustvar = c(-4, 5, -4, 5))

df2 <- data.frame(x2 = rnorm(100), y2 = rnorm(100))

ggplot(df2, aes(x2, y2)) + geom_point() +
  geom_text(data = annotations2, aes(x = xpos, y = ypos, hjust = hjustvar, vjust = vjustvar, label = annotateText))