如何在同一图表中绘制2条形图

时间:2018-05-03 12:37:59

标签: r

我有这种格式的数据框。我想绘制每个day_of_week的条形图重叠。

day_of_week clicks impressions
        <int>  <int>       <int>
1           0  65181     3778745
2           1  54658     2912405
3           2  50020     3016874

我正在使用此代码。但它给我一个错误:

ggplot(weekday_count, aes(x=day_of_week)) +                    # basic graphical object
  geom_bar(aes(y=clicks), colour="red") +  # first layer
  geom_bar(aes(y=impressions), colour="green")  # second layer

错误:stat_count()不得与y美学一起使用。

1 个答案:

答案 0 :(得分:3)

鉴于您的代码,我认为您正在寻找的是

dd = read.table(text = 'day_of_week clicks impressions

          0  65181     3778745
          1  54658     2912405
          2  50020     3016874', header = T)

dd = melt(dd, id.vars = 'day_of_week')

ggplot(data = dd, aes(x = day_of_week, y = value, fill = variable)) +
  geom_col(alpha = 0.5, position = 'identity')

enter image description here