使用 R 将带箭头的单箭头添加到 ggplot2 中的绘图
Adding a Single Arrow with Arrowhead to a plot in ggplot2 using R
我有一台 PC,64 位。在 Rstudio v0.98.953 中使用 ggplot 2,当前 R 更新。
我是ggplot新手,对R的熟练程度有限。我有以下代码:
# Simple Bar Plot for Multivariate OR by NVP quartile
dat <- data.frame(
QUAR = factor(c("1","2","3","4"), levels=c("1","2","3","4")),
aOR = c(1.00, 2.47, 3.33, 9.17),
lowerCI = c(1.00, 1.09, 1.33, 3.20),
upperCI = c(1.00, 5.60, 8.30, 26.0)
)
dat
library(ggplot2)
myplot = ggplot(data=dat, aes(x=QUAR, y=aOR, fill=QUAR)) +
geom_bar(colour="black", width=.8, stat="identity") +
scale_fill_manual(values=c("#e8e7e7", "#c3bfbf", "#908c8c", "#363434")) +
guides(fill=FALSE) +
xlab("XXX Quartile") + ylab("YYY") +
geom_text(data=dat, aes(x=QUAR, y=aOR, label=round(aOR, 1)), vjust=-0.5) +
coord_cartesian(ylim = c(0, 11)) +
ggtitle("Graph")
myplot
# this gets rid of the grid line and background color
plot (myplot)
myplot2 = myplot + geom_errorbar(aes(ymin=lowerCI, ymax=upperCI), width=.1) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
myplot2
这很好用,但我想在第 4 个栏中添加一个箭头。您可以看到 CI 超出了 Y 轴的上限(设置为 11),我只想放置一个向上的箭头以指示第 4 根柱线的上限超出11.
我已经尝试了箭头功能 - arrows(x0, y0, x1, y1, code=1),但无法将其设置为 运行(returns plot.new 还没有被调用或类似的东西)。
有什么想法吗?
谢谢!
geom_segment
有一个 arrow
论点应该可以帮助你。尝试这样的事情:
library(grid) ## Needed for `arrow()`
myplot2 + geom_segment(aes(x=4, xend=4, y=10, yend=11),
arrow = arrow(length = unit(0.5, "cm")))
我有一台 PC,64 位。在 Rstudio v0.98.953 中使用 ggplot 2,当前 R 更新。
我是ggplot新手,对R的熟练程度有限。我有以下代码:
# Simple Bar Plot for Multivariate OR by NVP quartile
dat <- data.frame(
QUAR = factor(c("1","2","3","4"), levels=c("1","2","3","4")),
aOR = c(1.00, 2.47, 3.33, 9.17),
lowerCI = c(1.00, 1.09, 1.33, 3.20),
upperCI = c(1.00, 5.60, 8.30, 26.0)
)
dat
library(ggplot2)
myplot = ggplot(data=dat, aes(x=QUAR, y=aOR, fill=QUAR)) +
geom_bar(colour="black", width=.8, stat="identity") +
scale_fill_manual(values=c("#e8e7e7", "#c3bfbf", "#908c8c", "#363434")) +
guides(fill=FALSE) +
xlab("XXX Quartile") + ylab("YYY") +
geom_text(data=dat, aes(x=QUAR, y=aOR, label=round(aOR, 1)), vjust=-0.5) +
coord_cartesian(ylim = c(0, 11)) +
ggtitle("Graph")
myplot
# this gets rid of the grid line and background color
plot (myplot)
myplot2 = myplot + geom_errorbar(aes(ymin=lowerCI, ymax=upperCI), width=.1) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
myplot2
这很好用,但我想在第 4 个栏中添加一个箭头。您可以看到 CI 超出了 Y 轴的上限(设置为 11),我只想放置一个向上的箭头以指示第 4 根柱线的上限超出11.
我已经尝试了箭头功能 - arrows(x0, y0, x1, y1, code=1),但无法将其设置为 运行(returns plot.new 还没有被调用或类似的东西)。
有什么想法吗?
谢谢!
geom_segment
有一个 arrow
论点应该可以帮助你。尝试这样的事情:
library(grid) ## Needed for `arrow()`
myplot2 + geom_segment(aes(x=4, xend=4, y=10, yend=11),
arrow = arrow(length = unit(0.5, "cm")))