ggplot2中位置闪避错误

时间:2014-04-30 10:07:31

标签: r ggplot2 bar-chart

我目前正面临使用ggplot2的错误。我想使用此数据框创建一个标准错误栏的条形图:

       mean        se       pattern quality
1 54.955357 19.792315        spread    good
2 54.506944 18.580981       clumped    good
3 29.604167 14.937291 centered good    good
5 23.300595 14.336305        spread     bad
6  8.371528  5.960366       clumped     bad
7 16.364583 11.525207 centered good     bad
8  7.062500 11.125915  centered bad     bad

我使用这个公式创建我的条形图:

ggplot(table, aes(x=pattern, y=mean, fill=quality))+
geom_bar(position="dodge")+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se, 
                  width=0.2, position=position_dodge(0.9)))

但是当我运行它时,应该有我的条形图的窗口显示为空白并且弹出此错误消息

Don't know how to automatically pick scale for object of type proto/environment. Defaulting to continuous
Error : Aesthetics must either be length one, or the same length as the dataProblems:position_dodge(0.9)

当我尝试在没有position=position_dodge(0.9)的情况下运行它时,会出现一个条形图,但条形图位于每个平均条之间而不是中间。

我已经为dodge和其他事情尝试了几个值,但我的想法已经不多了。

1 个答案:

答案 0 :(得分:5)

我收到一条警告,表示已应用“stat_identity”(映射到值,而不是计数)。要阻止该警告,只需将stat="identity"添加到geom。

te <- c("val mean se pattern quality", 
    "1 54.955357 19.792315 spread good", 
    "2 54.506944 18.580981 clumped good",
    "3 29.604167 14.937291 centered_good good",
    "5 23.300595 14.336305 spread bad",
    "6 8.371528  5.960366 clumped bad",
    "7 16.364583 11.525207 centered_good bad",
    "8 7.062500 11.125915 centered_bad bad")

df <- read.table(text=te, header=T)

require(ggplot2)

ggplot(df, aes(x=pattern, y=mean, fill=quality))+
  geom_bar(position="dodge", stat="identity")+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), 
            width=0.2, position=position_dodge(0.9))

enter image description here