CyberPunk
CyberPunk

Reputation: 1447

How to plot 2 bar plot in same graph

I have the dataframe in this format. I want to plot bar graph overlapping each other for each day_of_week.

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

I am using this code. But it throws me an error :

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

Error: stat_count() must not be used with a y aesthetic.

Upvotes: 0

Views: 90

Answers (1)

Felipe Alvarenga
Felipe Alvarenga

Reputation: 2652

given your code, i think what you are looking for is

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

Upvotes: 3

Related Questions