R& ggplot2:如何获取轴标签下的箭头? [英] R & ggplot2: How to get arrows under the axis label?

查看:233
本文介绍了R& ggplot2:如何获取轴标签下的箭头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我将用R / ggplot2绘制比值比,并且我想在X轴标签的下面或旁边添加两个箭头。一个指向左边,一个指向右边,表现出去/增加的影响力。我尝试了很多东西,比如geom_path,geom_line,但没有取得很大的成功。
这是我的代码:

  forest < -  function(d,xlab =Odds Ratio,ylab = 影响变量){
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)

##测试数据
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(数据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),箭头=箭头(长度=单位(0.15,厘米)),颜色=黑色,大小= 1)
带来了一些箭头,但他们与我的原始数据相冲突。

预先感谢您的解决方案,帮助或提示!

Marc

解决方案

这是一篇非常古老的文章,但我认为我会分享另一种选择,绊倒在这。



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

  p1 < -  qplot(x = 0: 12,y = 0:12,geom =blank)
p1 <-p1 + ylab(表达式(符号('\256')))
p1 < - p1 + xlab表达式(符号('\ 256')))
p1 < - p1 + theme(axis.title.y = element_text(size = 30))
p1 < - p1 + theme .title.x = element_text(size = 30))

p1


主题修改aren'绝对有必要,但你可能会想要至少增加箭头的大小。

默认情况下,符号()函数使用本文档中概述的Adobe Symbol代码: 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))



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:

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

解决方案

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  

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

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

这篇关于R&amp; ggplot2:如何获取轴标签下的箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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