R - ggplot2 - 如果 geom_errorbar 超出限制,则添加箭头
R - ggplot2 - Add arrow if geom_errorbar outside limits
我正在使用 ggplot 创建图形,并希望使用箭头指示我的误差线超出定义轴的位置。例如,我想最终得到一个如下所示的图形:
我想让 R 确定哪些下限在定义的图表范围之外,并添加一个漂亮的箭头(而不是我丑陋的油漆添加的箭头)。
我知道必须有办法做到这一点。有任何想法吗?这是我在没有手动添加箭头的情况下制作上图的代码:
#generate data
myData<-data.frame(ALPHA=round(runif(60,.5,.8),2),
error=round(runif(60,.05,.15),2),
formN=rep(1:5,12),
Cat=c(rep("ELL",30),rep("SWD",30)),
grade=rep(c(rep(3,5),rep(4,5),rep(5,5),rep(6,5),rep(7,5),rep(8,5)),2)
)
myData$LCL<-myData$ALPHA-myData$error
myData$UCL<-myData$ALPHA+myData$error
#set error outside of range for example
myData[myData$Cat=="ELL" & formN==1,"LCL"]<-0
library(ggplot2)
ggplot(myData, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3)
这个怎么样:首先创建一个列来检查值是否超出您的范围,如果超出范围,则确定从 y 点到绘图边界的长度。
library(dplyr)
myData_m <- myData %>% mutate(LCL_l = ifelse(LCL < .3, ALPHA - .3, NA), UCL_l = ifelse(UCL > 1, 1 - ALPHA, NA))
在第二步中使用此变量添加带有 segment
的箭头。如果还有值超过上限,您可以另外使用其他变量 ULC_l
添加更多箭头。
ggplot(myData_m, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3) +
geom_segment(aes(x = formN - .12, xend = formN - .12, y = ALPHA, yend = ALPHA - LCL_l), arrow = arrow(length = unit(myData_m$LCL_l, "cm")))
P.S.: -.12
用于去除箭矢的闪避效果
我正在使用 ggplot 创建图形,并希望使用箭头指示我的误差线超出定义轴的位置。例如,我想最终得到一个如下所示的图形:
我想让 R 确定哪些下限在定义的图表范围之外,并添加一个漂亮的箭头(而不是我丑陋的油漆添加的箭头)。
我知道必须有办法做到这一点。有任何想法吗?这是我在没有手动添加箭头的情况下制作上图的代码:
#generate data
myData<-data.frame(ALPHA=round(runif(60,.5,.8),2),
error=round(runif(60,.05,.15),2),
formN=rep(1:5,12),
Cat=c(rep("ELL",30),rep("SWD",30)),
grade=rep(c(rep(3,5),rep(4,5),rep(5,5),rep(6,5),rep(7,5),rep(8,5)),2)
)
myData$LCL<-myData$ALPHA-myData$error
myData$UCL<-myData$ALPHA+myData$error
#set error outside of range for example
myData[myData$Cat=="ELL" & formN==1,"LCL"]<-0
library(ggplot2)
ggplot(myData, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3)
这个怎么样:首先创建一个列来检查值是否超出您的范围,如果超出范围,则确定从 y 点到绘图边界的长度。
library(dplyr)
myData_m <- myData %>% mutate(LCL_l = ifelse(LCL < .3, ALPHA - .3, NA), UCL_l = ifelse(UCL > 1, 1 - ALPHA, NA))
在第二步中使用此变量添加带有 segment
的箭头。如果还有值超过上限,您可以另外使用其他变量 ULC_l
添加更多箭头。
ggplot(myData_m, aes(x=formN, y=ALPHA, colour=Cat)) +
geom_errorbar(aes(ymin=LCL, ymax=UCL), width=.4, position=position_dodge(.5)) +
geom_point(position=position_dodge(.5), size=2) +
labs(x="Form", y="Alpha", title="TITLE") +
geom_line(position=position_dodge(.5), size=.3) +
coord_cartesian(ylim=c(.3, 1)) +
facet_wrap(~grade, ncol=3) +
geom_segment(aes(x = formN - .12, xend = formN - .12, y = ALPHA, yend = ALPHA - LCL_l), arrow = arrow(length = unit(myData_m$LCL_l, "cm")))
P.S.: -.12
用于去除箭矢的闪避效果