我有一台64位的PC.在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
Run Code Online (Sandbox Code Playgroud)

这很好,但我想在第4个栏中添加一个箭头.您可以看到CI超出了Y轴的上限(设置为11),我只想将一个箭头指向上方,以指示第4个柱的上限超过11.
我已经尝试过箭头功能 - 箭头(x0,y0,x1,y1,代码= 1),但无法让它运行(返回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")))
Run Code Online (Sandbox Code Playgroud)
