如何绘制带有排序水平误差线的图表(带有错误标记的排序条形图)?

发布于 2024-08-24 01:25:43 字数 1006 浏览 5 评论 0原文

我想将平均值和标准误差绘制为水平条形图,并且我希望对平均值进行排序。

我找到了使用格子绘制水平排序条形图的方法,但我不知道如何添加错误标记。以下是我的数据和我想出的 R 代码。

data <- structure(c(0.67, 0.67, 0.76, 0.66, 0.71, 0.6, 0.52, 0.6, 0.71, 0.76, 
0.76, 0.71, 0.6, 0.61, 0.9, 0.5, 0.58, 0.84, 0.68, 0.88,
0.89, 0.96, 1, 0.95, 1, 1, 0.98, 0.78, 0.98, 1, 
1, 0.99, 1, 1, 0.95, 0.92, 1, 0.91, 1, 0.87, 
0.91, 0.72, 0.73, 0.55, 0.82, 0.87, 0.64, 0.75, 0.75, 1, 
0.81, 0.79, 1, 0.74, 0.57, 0.84, 1, 0.95, 0.78, 0.95), .Dim = c(20L, 3L), .Dimnames = list(
    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20"), c("A", 
    "B", "C")))

means <- apply(data, 2, mean)

errors <- apply(data, 2, sd)

plot.data <- data.frame(colnames(data), means, errors)

colnames(plot.data) <- c("var", "mean", "error")

library("lattice")
plot.new()

barchart(reorder(var, mean) ~ mean, plot.data, xlim = c(0, 1))

有什么方法可以在此图表中添加错误标记吗?如果没有,关于如何在 R 中绘制我想要的图表有什么建议吗?

先感谢您!

I would like to plot means and standard errors as a horizontal barchart, and I want the mean sorted.

I've found the way to plot horizontal sorted barcharts using lattice, but I do not know how to add error marks. The following are my data and the R code I came up with.

data <- structure(c(0.67, 0.67, 0.76, 0.66, 0.71, 0.6, 0.52, 0.6, 0.71, 0.76, 
0.76, 0.71, 0.6, 0.61, 0.9, 0.5, 0.58, 0.84, 0.68, 0.88,
0.89, 0.96, 1, 0.95, 1, 1, 0.98, 0.78, 0.98, 1, 
1, 0.99, 1, 1, 0.95, 0.92, 1, 0.91, 1, 0.87, 
0.91, 0.72, 0.73, 0.55, 0.82, 0.87, 0.64, 0.75, 0.75, 1, 
0.81, 0.79, 1, 0.74, 0.57, 0.84, 1, 0.95, 0.78, 0.95), .Dim = c(20L, 3L), .Dimnames = list(
    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", "17", "18", "19", "20"), c("A", 
    "B", "C")))

means <- apply(data, 2, mean)

errors <- apply(data, 2, sd)

plot.data <- data.frame(colnames(data), means, errors)

colnames(plot.data) <- c("var", "mean", "error")

library("lattice")
plot.new()

barchart(reorder(var, mean) ~ mean, plot.data, xlim = c(0, 1))

Is there any way to add error marks to this chart? If not, any suggestion on how to plot the chart I want in R?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-08-31 01:25:43

请参阅 R-帮助:向晶格添加误差线绘图

prepanel.ci <- function(x, y, lx, ux, subscripts, ...) {
    x <- as.numeric(x)
    lx <- as.numeric(lx[subscripts])
    ux <- as.numeric(ux[subscripts])
    list(xlim = range(0, x, ux, lx, finite = TRUE))
}


panel.ci <- function(x, y, lx, ux, subscripts, ...) {
    x <- as.numeric(x)
    y <- as.numeric(y)
    lx <- as.numeric(lx[subscripts])
    ux <- as.numeric(ux[subscripts])
    panel.barchart(x, y, ...)
    panel.arrows(lx, y, ux, y, col = 'black',
                 length = 0.25, unit = "native",
                 angle = 90, code = 3)
}

p <- barchart(reorder(var, mean) ~ mean, data=plot.data,
              lx=plot.data$mean-plot.data$error,
              ux=plot.data$mean+plot.data$error,
              panel=panel.ci,
              prepanel=prepanel.ci)
print(p)

带误差条的格子条形图

See R-Help: Adding error bars to lattice plots

prepanel.ci <- function(x, y, lx, ux, subscripts, ...) {
    x <- as.numeric(x)
    lx <- as.numeric(lx[subscripts])
    ux <- as.numeric(ux[subscripts])
    list(xlim = range(0, x, ux, lx, finite = TRUE))
}


panel.ci <- function(x, y, lx, ux, subscripts, ...) {
    x <- as.numeric(x)
    y <- as.numeric(y)
    lx <- as.numeric(lx[subscripts])
    ux <- as.numeric(ux[subscripts])
    panel.barchart(x, y, ...)
    panel.arrows(lx, y, ux, y, col = 'black',
                 length = 0.25, unit = "native",
                 angle = 90, code = 3)
}

p <- barchart(reorder(var, mean) ~ mean, data=plot.data,
              lx=plot.data$mean-plot.data$error,
              ux=plot.data$mean+plot.data$error,
              panel=panel.ci,
              prepanel=prepanel.ci)
print(p)

lattice barchart with error bar

故乡的云 2024-08-31 01:25:43

如果它不必是点阵,这里是一个使用基本 R 功能的简单函数,它提供三个参数:条形的宽度 (xv)、误差条的长度(向上和向下)(z)以及 y 轴 (nn) 上条形的标签。

error.bars<-function(xv,z,nn){
par(las = 1)
yv <- barplot(xv,horiz = TRUE,col="cyan",xlim=c(0,(max(xv)+max(z))),names=nn,xlab=deparse(substitute(xv)))
g <- (max(yv)-min(yv))/(3*length(yv)) 
for (i in 1:length(yv)) {
lines(c(xv[i]+z[i],xv[i]-z[i]),c(yv[i],yv[i]))
lines(c(xv[i]+z[i],xv[i]+z[i]),c(yv[i]+g,yv[i]-g))
lines(c(xv[i]-z[i],xv[i]-z[i]),c(yv[i]+g,yv[i]-g))
}}

plot.data <- plot.data[order(plot.data$mean),] # reorder data
mean<-as.vector(plot.data$mean)
se<-as.vector(plot.data$error)
labels<-as.character(plot.data$var)

error.bars(mean,se,labels)

替代文本

If it doesn't have to be lattice here is a simple function that uses base R functionality, that is supplied with three arguments: the widths of the bars (xv), the lengths (up and down) of the error bars (z) and the labels for the bars on the y axis (nn).

error.bars<-function(xv,z,nn){
par(las = 1)
yv <- barplot(xv,horiz = TRUE,col="cyan",xlim=c(0,(max(xv)+max(z))),names=nn,xlab=deparse(substitute(xv)))
g <- (max(yv)-min(yv))/(3*length(yv)) 
for (i in 1:length(yv)) {
lines(c(xv[i]+z[i],xv[i]-z[i]),c(yv[i],yv[i]))
lines(c(xv[i]+z[i],xv[i]+z[i]),c(yv[i]+g,yv[i]-g))
lines(c(xv[i]-z[i],xv[i]-z[i]),c(yv[i]+g,yv[i]-g))
}}

plot.data <- plot.data[order(plot.data$mean),] # reorder data
mean<-as.vector(plot.data$mean)
se<-as.vector(plot.data$error)
labels<-as.character(plot.data$var)

error.bars(mean,se,labels)

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文