如何从离散对象列表中对分组条形图中的类别进行分组?

时间:2014-10-02 16:04:07

标签: r

让我们说我想制作一个不同事物平均重量的情节。一类是动物,另一类是汽车。在动物类别中,我有一只猫,一条狗和一条金鱼,在汽车类别中,我有一辆普锐斯和卡车。我该如何分组动物'重量和汽车的重量和#39;在R的条形图上加权?我所看到的每个例子都是分组的条形图'拥有预计属于每个类别的组。

到目前为止,我是如何尝试这样做的:

d <- data.frame(categories = c("animal", "animal", "animal","car", "car"),
        centers = c("dog", "cat", "fish", "prius", "truck"),
        means = c(23, 24, 28, 19, 40),
        standardErrors = c(1.2, 1.7, 0.9, 0.4, 1),
        mins = c(21, 20, 20, 16, 30),
        maxes = c(27, 29, 30, 32, 44),
        stringsAsFactors = FALSE)

require(ggplot2)
ggplot(d, aes(x=categories, y=means, fill=centers)) + geom_bar(position=position_dodge())

我收到以下错误:Error: ggplot2 doesn't know how to deal with data of class数字

1 个答案:

答案 0 :(得分:1)

以下作品:

ggplot(d, aes(x=categories, y=means, fill=centers)) + 
    geom_bar(stat='identity',position=position_dodge())

enter image description here

您需要在geom_bar

中添加:stat ='identity'