label <- paste0("X", 1:6)
mean <- c(1.29,0.76,2.43,1.68,1.22,1.7)
lower <- c(0.84,0.50,1.58,1.1,0.8,1.11)
upper <- c(1.95,1.16,4.5,2.54,1.85,2.56)
df <- data.frame(label, mean, lower, upper)
# reverses the factor level ordering for labels after coord_flip()
# df$label <- factor(df$label, levels=rev(df$label))
library(ggplot2)
fp <- ggplot(data=df, aes(x=label, y=mean, ymin=lower, ymax=upper)) +
geom_errorbar() + geom_point() +
geom_hline(yintercept=1, lty=2) + # add a dotted line at x=1 after flip
coord_flip() + # flip coordinates (puts labels on y axis)
xlab("Label") + ylab("Mean (95% CI)") +
theme_bw() + ylim(c(0, 4))
fp
使用上面的代码,X3
由于其上限为 4.5,大于ylim(c(0, 4))
I 设置,因此被截断。有没有办法在遵守我设置的轴限制的同时防止这种情况发生?
也就是说,我想要这样的情节: