将ggplot文字置于每个角落 [英] Position ggplot text in each corner

查看:181
本文介绍了将ggplot文字置于每个角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平线和一条垂直线的散点图,描绘了阈值,因此它们将图分成四个象限。我想标记象限。我认为做这件事的最好方法是在图表的每个角落都有一个数字(欢迎替代建议!)。

我已经设置了一个文字到每个象限的角落,但是位置并不完美。我认为这个问题与轴的缩放比例不同有关(值的范围大致相同,但我的身材的宽度大约是身高的三倍)。

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

  plot.build = ggplot_build(plot)

xpos =数字(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 值将文本放置在角落,然后 hjust vjust 在geom_text中的参数来定位图中的文本。使用 hjustvar vjustvar 将它们放置在图的更外部或外部。

正如@baptiste提到的那样,最好为注释使用新的数据集

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

注释< - data.frame(
xpos = c(-Inf,-Inf,Inf,Inf),
ypos = c(-Inf,Inf,-Inf,Inf),
annotateText = c(Bottom Left(h0, v0),左上(h0,v1)
,右下h1,v0,右上h1,v1),
hjustvar = c(0,0,1,1 ),
vjustvar = c(0,1,0,1))#< - adjust


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



如果我们想更改任何文本位置,我们会调整视野 hjustvar 和垂直位置 vjustvar

 #如何调整头寸(远离角落)
注解$ hjustvar <-c(0,-1.5,1,2.5)#更高的值=右边,较低的值=左边
注解$ vjustvar <-c(0,1,0,1)#高值=上,低值=下

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



希望这作品!


I've got a scatter plot with a horizontal and a vertical line, which depict thresholds values, and therefore they divide the plot into four quadrants. I'd like to label the quadrants. I think the best way to do this would be a number in each of the four corners of the graph (alternative suggestions are welcome!).

I've managed to put a text into the corner of each quadrant, but the positions are not perfect. I assume that the problem has to do with the fact that the scaling of the axes is different (the range of values is about the same, but the width of my figure is about three times the height).

Currently I proceed the following way. First I create the graph with the points and the two lines, then I build it in order to get the range of the two axes, which I use in order to adjust the position of the texts.

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)

Basically this works, but due to the scaling the space between the numbers and the borders of the plot are not the same for both axes. I've tried to adjust the x position of the text, but then ggplot just expands the range of values, the positions (relative to the borders) stay the same. Is there a way to move the text without changing the range of values?

Thanks in advance!

解决方案

This example uses the Inf & -Inf values to position the text at the corners and then hjust and vjust arguments in the geom_text to position the text inside the plot. Use the hjustvar and vjustvar to position them further into or outside the plot.

As mentioned by @baptiste it's best to use a new data set for the annotations

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(aes(x=xpos,y=ypos,hjust=hjustvar,vjust=vjustvar,label=annotateText))

If we wanted to change any of the text positions, we would adjust the horizontal positions with hjustvar and the vertical positions with vjustvar.

# How To Adjust positions (away from corners)
annotations$hjustvar<-c(0,  -1.5,  1,  2.5)  # higher values = right, lower values = left 
annotations$vjustvar<-c(0,1,0,1) # higher values = up, lower values = down 

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

Hope this works!

这篇关于将ggplot文字置于每个角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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