R & ggplot2:如何得到箭头下的轴标签?

时间:2021-09-29 14:57:27


I'm about to plot odds ratios with R/ggplot2 and I want to add two arrows underneath or next to the X-axis label. One pointing to the left, one pointing to the right, showing de-/increasing influence. I've tried lots of things, like geom_path, geom_line, without great success. This is my code:

我将要绘制R/ggplot2的比值我想在x轴标签下面或旁边添加两个箭头。一个指向左边,一个指向右边,显示出de-/增加的影响。我尝试过很多东西,比如土地网,土地网,但都没有成功。这是我的代码:

forest <- function(d, xlab="Odds Ratio", ylab="Influencing variables"){
require(ggplot2)
p <- ggplot(d, aes(x=ordered(x, levels=rev(x)), y=y, ymin=ylo, ymax=yhi)) +
geom_pointrange() +
geom_segment(aes(x = 0, xend = 0, y= 1, yend= 2)) +
coord_flip() +
geom_hline(aes(yintercept=1), lty=2) +
ylab(xlab) +
xlab(ylab) +
scale_y_log10() 
return(p)
}
##Test Data
data <- data.frame( x   = c("A","B","C","D","E","F","G","H","I"),
                y   = c(1.782,0.136,0.978,0.645,0.518,1.474,0.855,0.673,0.369))
data <- transform(data, ylo = (0.719,0.046,0.945,0.295,0.188,0.577,0.407,0.310,0.145), 
        yhi = c(4.420,0.398,1.012,1.411,1.424,3.768,1.798,1.460,0.940))
forest(data)

Adding a line like geom_line(aes(x=1), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1) brings some arrows but they collide with my original data.
Thanks in advance for your solution, help or hint!
Marc

添加像geom_line(aes(x=1)、arrow(length=unit(0.15,“cm”)、colour=“black”、size=1)这样的行会带来一些箭头,但它们会与我的原始数据发生冲突。提前感谢您的解决方案、帮助或提示!马克

3 个解决方案

#1


5  

This is a pretty old post, but thought I would share another option for anyone who stumbles upon this.

这是一个相当古老的帖子,但我认为我将分享另一个选项给任何偶然发现这一点的人。

I have a situation which really requires that I have arrows outside of the plotting region (and don't care to involve grid). As shown below, using expressions and symbols might be a good option for some:

我有一种情况,我需要在绘图区域之外有箭头(不关心网格)。如下所示,使用表达式和符号可能是一些人的好选择:

p1 <- qplot(x = 0:12, y = 0:12, geom = "blank") 
p1 <- p1 + ylab(expression(symbol('\256')))
p1 <- p1 + xlab(expression(symbol('\256')))
p1 <- p1 + theme(axis.title.y = element_text(size = 30))
p1 <- p1 + theme(axis.title.x = element_text(size = 30))

p1  

R & ggplot2:如何得到箭头下的轴标签?

The theme modifications aren't absolutely necessary but you will probably want to increase the size of the arrows at least a bit.

主题修改不是绝对必要的,但是您可能希望至少增加一些箭头的大小。

By default, the symbol() function takes Adobe Symbol codes which are outlines in this document: http://goo.gl/vRzpJU

默认情况下,symbol()函数接受Adobe符号代码,该代码在本文中概述为:http://goo.gl/vRzpJU

If you want to add some text to the labels (just adding to the expression would probably make it too big), the following should get you started:

如果您想在标签中添加一些文本(只是添加到表达式中可能会使它太大),下面应该开始:

grid.text(label="X", x = 0.645, y = 0.02)
grid.text(label="Y", x = 0.1, y = 0.5, rot=90)
grid.gedit("GRID.text", gp=gpar(fontsize=15))

#2


3  

This can easily be done using mathematical annotation in R expression(x <-> y). A table of available options can be found here. expression also allows latex like math symbols.

使用R表达式(x <-> y)中的数学注释很容易做到这一点。表达式也允许像数学符号一样的乳胶。

p1 <- qplot(x = 1:10, y = 1:10, geom = "blank") 
p1 <- p1 + xlab(expression(decreasing %<->% increasing))
p1 <- p1 + ylab(expression(down %->% up))
p1 <- p1 + ggtitle(expression("Darcy's law" %->% q == -k*frac(Delta*h,Delta*l)))
p1 <- p1 + theme_bw(base_size=14)

R & ggplot2:如何得到箭头下的轴标签?

#3


2  

I'm doing a bit of guessing about what you want, exactly, as I'm not sure what you were attempting with geom_segment, whether that was part of your data, or an attempt to create an arrow. But you can build on geom_segment to get something like what you describe:

我正在猜测您想要什么,确切地说,因为我不确定您正在尝试使用geom_segment做什么,不管它是您的数据的一部分,还是创建箭头的尝试。但是你可以在geom_segment上添加如下内容:

#Your test data, with a small typo repaired
dd <- data.frame(x = c("A","B","C","D","E","F","G","H","I"),
                 y = c(1.782,0.136,0.978,0.645,0.518,1.474,0.855,0.673,0.369))
dd <- transform(dd, ylo = c(0.719,0.046,0.945,0.295,0.188,0.577,0.407,0.310,0.145), 
        yhi = c(4.420,0.398,1.012,1.411,1.424,3.768,1.798,1.460,0.940))

#Create a separate data frame for the arrow labels
arrowLab <- data.frame(lab = c("Increasing","Decreasing"),
                       x = c(0.15,0.15),y = c(10^0.25,10^(-0.25)))

ggplot(data = dd, aes(x=ordered(x, levels=rev(x)), y=y)) +
        geom_pointrange(aes(ymin=ylo, ymax=yhi)) +
        geom_segment(aes(x = 0, xend = 0, y= 1, yend= 2),
                         arrow=arrow(length=unit(0.2,"cm"))) +
        geom_segment(aes(x = 0, xend = 0, y= 1, yend= 10^(-0.25)),
                         arrow=arrow(length=unit(0.2,"cm"))) +
        geom_text(data = arrowLab,aes(x=x,y=y,label = lab),size = 3) +
        coord_flip() +
        geom_hline(aes(yintercept=1), lty=2) +
        scale_y_log10() 

R & ggplot2:如何得到箭头下的轴标签?

Note that I hard-coded much of the positioning, so you'll likely want to tinker with those values to get what you want, or you could try to choose them programmatically based on your data.

注意,我硬编码了大部分的定位,所以您可能想要修改这些值以得到您想要的,或者您可以尝试根据您的数据以编程方式选择它们。

#1


5  

This is a pretty old post, but thought I would share another option for anyone who stumbles upon this.

这是一个相当古老的帖子,但我认为我将分享另一个选项给任何偶然发现这一点的人。

I have a situation which really requires that I have arrows outside of the plotting region (and don't care to involve grid). As shown below, using expressions and symbols might be a good option for some:

我有一种情况,我需要在绘图区域之外有箭头(不关心网格)。如下所示,使用表达式和符号可能是一些人的好选择:

p1 <- qplot(x = 0:12, y = 0:12, geom = "blank") 
p1 <- p1 + ylab(expression(symbol('\256')))
p1 <- p1 + xlab(expression(symbol('\256')))
p1 <- p1 + theme(axis.title.y = element_text(size = 30))
p1 <- p1 + theme(axis.title.x = element_text(size = 30))

p1  

R & ggplot2:如何得到箭头下的轴标签?

The theme modifications aren't absolutely necessary but you will probably want to increase the size of the arrows at least a bit.

主题修改不是绝对必要的,但是您可能希望至少增加一些箭头的大小。

By default, the symbol() function takes Adobe Symbol codes which are outlines in this document: http://goo.gl/vRzpJU

默认情况下,symbol()函数接受Adobe符号代码,该代码在本文中概述为:http://goo.gl/vRzpJU

If you want to add some text to the labels (just adding to the expression would probably make it too big), the following should get you started:

如果您想在标签中添加一些文本(只是添加到表达式中可能会使它太大),下面应该开始:

grid.text(label="X", x = 0.645, y = 0.02)
grid.text(label="Y", x = 0.1, y = 0.5, rot=90)
grid.gedit("GRID.text", gp=gpar(fontsize=15))

#2


3  

This can easily be done using mathematical annotation in R expression(x <-> y). A table of available options can be found here. expression also allows latex like math symbols.

使用R表达式(x <-> y)中的数学注释很容易做到这一点。表达式也允许像数学符号一样的乳胶。

p1 <- qplot(x = 1:10, y = 1:10, geom = "blank") 
p1 <- p1 + xlab(expression(decreasing %<->% increasing))
p1 <- p1 + ylab(expression(down %->% up))
p1 <- p1 + ggtitle(expression("Darcy's law" %->% q == -k*frac(Delta*h,Delta*l)))
p1 <- p1 + theme_bw(base_size=14)

R & ggplot2:如何得到箭头下的轴标签?

#3


2  

I'm doing a bit of guessing about what you want, exactly, as I'm not sure what you were attempting with geom_segment, whether that was part of your data, or an attempt to create an arrow. But you can build on geom_segment to get something like what you describe:

我正在猜测您想要什么,确切地说,因为我不确定您正在尝试使用geom_segment做什么,不管它是您的数据的一部分,还是创建箭头的尝试。但是你可以在geom_segment上添加如下内容:

#Your test data, with a small typo repaired
dd <- data.frame(x = c("A","B","C","D","E","F","G","H","I"),
                 y = c(1.782,0.136,0.978,0.645,0.518,1.474,0.855,0.673,0.369))
dd <- transform(dd, ylo = c(0.719,0.046,0.945,0.295,0.188,0.577,0.407,0.310,0.145), 
        yhi = c(4.420,0.398,1.012,1.411,1.424,3.768,1.798,1.460,0.940))

#Create a separate data frame for the arrow labels
arrowLab <- data.frame(lab = c("Increasing","Decreasing"),
                       x = c(0.15,0.15),y = c(10^0.25,10^(-0.25)))

ggplot(data = dd, aes(x=ordered(x, levels=rev(x)), y=y)) +
        geom_pointrange(aes(ymin=ylo, ymax=yhi)) +
        geom_segment(aes(x = 0, xend = 0, y= 1, yend= 2),
                         arrow=arrow(length=unit(0.2,"cm"))) +
        geom_segment(aes(x = 0, xend = 0, y= 1, yend= 10^(-0.25)),
                         arrow=arrow(length=unit(0.2,"cm"))) +
        geom_text(data = arrowLab,aes(x=x,y=y,label = lab),size = 3) +
        coord_flip() +
        geom_hline(aes(yintercept=1), lty=2) +
        scale_y_log10() 

R & ggplot2:如何得到箭头下的轴标签?

Note that I hard-coded much of the positioning, so you'll likely want to tinker with those values to get what you want, or you could try to choose them programmatically based on your data.

注意,我硬编码了大部分的定位,所以您可能想要修改这些值以得到您想要的,或者您可以尝试根据您的数据以编程方式选择它们。