Using ggplot2
, 这很容易。您可以完全控制误差条所有四个“边”的长度。和geom_errorbar()
您设置 y 错误,并且geom_errobarh()
(h 代表水平)您设置 x 误差。
#toy data
df <- data.frame(X = rnorm(4), errX = rnorm(4)*0.1, Y = rnorm(4), errY = rnorm(4)*0.2)
#load ggplot2
require(ggplot2)
#make graph
ggplot(data = df, aes(x = X, y = Y)) + geom_point() + #main graph
geom_errorbar(aes(ymin = Y-errY, ymax = Y+errY)) +
geom_errorbarh(aes(xmin = X-errX, xmax = X+errX))
您可以通过设置内部参数来单独控制每个条形的颜色、线宽等geom_errorbar()
。详细信息请参阅帮助和 Google。例如,您可以控制“帽子”的宽度或使用以下命令完全消除它们width
范围。将上图与此图进行比较,了解删除它们的示例:
ggplot(data = df, aes(x = X, y = Y)) + geom_point() +
geom_errorbar(aes(ymin = Y-errY, ymax = Y+errY), width = 0) +
geom_errorbarh(aes(xmin = X-errX, xmax = X+errX), height = 0)