gpt4 book ai didi

R & ggplot2 : How to get arrows under the axis label?

转载 作者:行者123 更新时间:2023-12-04 16:36:20 26 4
gpt4 key购买 nike

我即将用 R/ggplot2 绘制优势比,我想在 X Axis 标签下方或旁边添加两个箭头。一个指向左边,一个指向右边,显示出减少/增加的影响。我尝试了很多东西,比如 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")), colour="black", size=1)
带来了一些箭头,但它们与我的原始数据相撞。
在此先感谢您的解决方案、帮助或提示!
马克

最佳答案

这可以使用 R 中的数学注释轻松完成 expression(x <-> y) .可以找到可用选项表 here . expression还允许像数学符号这样的 latex 。

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

关于R & ggplot2 : How to get arrows under the axis label?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7160706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com