【问题标题】:Error with position dodge in ggplot2ggplot2中的位置闪避错误
【发布时间】:2014-04-30 10:07:31
【问题描述】:

我目前在使用 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 和其他东西尝试了几个值,但我的想法已经不多了。

【问题讨论】:

  • 尝试在最后一行的aes 规范之外添加position=position_dodge(0.9)
  • 因为width 没有被变量改变,你也应该把它放在aes 规范之外
  • 太棒了!它奏效了,很好。发生了另一个错误(“ggplot2 不知道如何处理数字类的数据”),我正在寻找解决方案,但希望之后一切都会好起来。谢谢!

标签: r ggplot2 bar-chart


【解决方案1】:

我收到一条警告,指出应用了“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))

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多