如何绘制带有排序的水平误差条(带有错误标记的排序的条形图)的图表? [英] How to draw a chart with sorted horizontal error bars (sorted barcharts with error marks)?

查看:92
本文介绍了如何绘制带有排序的水平误差条(带有错误标记的排序的条形图)的图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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))

有什么方法可以向该图表添加错误标记吗?如果没有,关于如何在R中绘制所需图表的任何建议?

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?

提前谢谢!

推荐答案

请参阅R-Help:

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)

这篇关于如何绘制带有排序的水平误差条(带有错误标记的排序的条形图)的图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆