0

I have a dataframe as below (very simple structure) and I want to draw a column chart to show the amount for each date. The issue is that the date has duplicate entries (e.g., 2020-01-15).

  # A tibble: 5 x 2
  date       amount
  <date>      <dbl>
1 2020-01-02  4000 
2 2020-01-06  2568.
3 2020-01-15  2615.
4 2020-01-15  2565 
5 2020-01-16  2640 

When I try doing the following it somehow groups the similar dates together and draws a stacked column chart which is NOT what I want.

df %>%  
    ggplot(aes(x= factor(date), y=amount)) +
    geom_col()
    scale_x_discrete( labels = df$date ) #this creates discrete x-axis labels but the values are still stacked. So it just messes things up.

There's no issue if i'm using geom_line() but I want to see a bar for each date. Any idea how to do this?

samsamara
  • 4,630
  • 7
  • 36
  • 66
  • Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data in a plain text format - for example the output from `dput(yourdata)`. We cannot copy/paste data from images. – neilfws Dec 16 '20 at 02:05
  • @neilfws I have edited my question as per your comment. Thanks. – samsamara Dec 16 '20 at 02:15
  • can someone tell me why this question has been negged? – samsamara Dec 16 '20 at 02:23

1 Answers1

1

Try:

df %>% 
  ggplot(aes(date, amount)) +
  geom_col(position = position_dodge2()) + 
  scale_x_date(breaks = unique(df$date))

Result:

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63