ggplot2绘图之误差棒图
1.geom_errorbar()
geom_crossbar(mapping = NULL, data = NULL, stat = "identity",position = "identity", ..., fatten = 2.5, na.rm = FALSE,show.legend = NA, inherit.aes = TRUE)
geom_errorbar(mapping = NULL, data = NULL, stat = "identity",position = "identity", ..., na.rm = FALSE, show.legend = NA,inherit.aes = TRUE)
geom_linerange(mapping = NULL, data = NULL, stat = "identity",position = "identity", ..., na.rm = FALSE, show.legend = NA,inherit.aes = TRUE)
geom_pointrange(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., fatten = 4, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
aes(x,ymin,ymax,alpha,color,group,linetype,size)
x, ymin, ymax为必须设置的参数。
例:
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
upper = c(1.1, 5.3, 3.3, 4.2),
lower = c(0.8, 4.6, 2.4, 3.6)
)
p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_linerange(aes(ymin = lower, ymax = upper))
p + geom_pointrange(aes(ymin = lower, ymax = upper))
p + geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2)
p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)