如何使用ggplot绘制不同类别​​的彩色条

时间:2014-07-02 17:46:18

标签: r ggplot2

我的数据如下:

       id variable value
1   11149   a      500
2      91   b      1000
3   43233   a      500
4   42499   c      10
5  163993   c      30

我想绘制一个条形图,其中id为x,值的总和为y,每个条形图按变量填充不同的颜色。

我把代码编写为

ggplot(data2,aes(id,value))+
  geom_bar(aes(fill=variable),stat="identity")

效果不佳,并发出警告

Warning message:
position_stack requires constant width: output may be incorrect  

1 个答案:

答案 0 :(得分:0)

尝试将id转换为系数:

ggplot(data2, aes(factor(id), value)) +
  geom_bar(aes(fill=variable), stat="identity")
相关问题