如何在geom_col()中为重复的x轴值设置单独的列? [英] How to have separate columns for duplicate x-axis values in geom_col()?
本文介绍了如何在geom_col()中为重复的x轴值设置单独的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框(非常简单的结构),我想绘制一个柱形图以显示每个日期的金额.问题在于 date
有重复的条目(例如 2020-01-15
).
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.
如果我使用的是 geom_line()
,这没有问题,但是我希望看到每个日期的栏.知道怎么做吗?
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?
推荐答案
尝试:
df %>%
ggplot(aes(date, amount)) +
geom_col(position = position_dodge2()) +
scale_x_date(breaks = unique(df$date))
结果:
这篇关于如何在geom_col()中为重复的x轴值设置单独的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文