R& ggplot2:如何在轴标签下获得箭头?

时间:2011-08-23 12:00:22

标签: r ggplot2 axis

我即将用R / ggplot2绘制比值比,我想在X轴标签下面或旁边添加两个箭头。一个指向左侧,一个指向右侧,显示减少/增加的影响。我尝试过很多东西,比如geom_path,geom_line,没有太大的成功。 这是我的代码:

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)

添加像这样的行     geom_line(aes(x = 1),arrow = arrow(length = unit(0.15,“cm”)),color =“black”,size = 1) 带来一些箭头,但它们与我的原始数据相撞。
在此先感谢您的解决方案,帮助或提示!
马克

3 个答案:

答案 0 :(得分:6)

这是一篇相当古老的帖子,但我想我会为偶然发现这个问题的人分享另一个选择。

我的情况确实需要我在绘图区域之外有箭头(并且不关心grid)。如下所示,对于某些人来说,使用表达式和符号可能是一个不错的选择:

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  

enter image description here

主题修改并非绝对必要,但您可能希望至少稍微增加箭头的大小。

默认情况下,symbol()函数采用本文档中概述的Adobe符号代码:http://goo.gl/vRzpJU

如果你想在标签上添加一些文字(只是添加到表达式可能会使它太大),以下内容应该让你开始:

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))

答案 1 :(得分:6)

这可以使用R expression(x <-> y)中的数学注释轻松完成。可以找到可用选项表hereexpression也允许乳胶像数学符号。

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)

Including arrows in plot labels

答案 2 :(得分:2)

我正在猜测你想要什么,确切地说,因为我不确定你用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() 

enter image description here

请注意,我对大部分定位进行了硬编码,因此您可能希望修改这些值以获得所需内容,或者您​​可以尝试根据数据以编程方式选择它们。