2014-10-02 22 views
0

比方说,我想制作不同事物的平均重量的barplot。一类是动物,另一类是汽车。在动物类别中,我有一只猫,一条狗和一条金鱼,而在汽车类别中,我有一辆普锐斯和卡车。我如何将动物的重量和汽车的重量组合在R的一个barplot上?我所看到的每一个'分组bar bar'的例子都有一些预计会出现在每个类别中的小组。如何从离散对象列表中对分组的条形图中的类别进行分组?

下面是我试图做到这一点至今:

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

以下工作:

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

enter image description here

您需要将:stat ='identity'添加到geom_bar

+0

谢谢,这就是我一直在寻找的! – Dave 2014-10-02 16:26:07

相关问题