隐藏x轴ggplot2中缺少的日期
我正在尝试在 ggplot2(条形图)
中制作图表。我的数据集中的数据点之间有几天的时间。我只想绘制变量所在的日期,并省略空日期。
I am trying to make a chart in ggplot2 (barplot)
. There are several days in between data points in my data set. I'd like to only graph the dates where variables are and omit the empty dates.
我一直在寻找一个小时,试图找到答案。我在这里找到了一些答复,指出解决方法是这样的:
I have been searching for an hour trying to find an answer to this. I found some replies here indicating that the fix is this:
scale_x_date(format = '%d%b', major='days')
但是, scale_x_date 内的那些参数似乎没有工作了。有人可以快速便捷地解决问题吗?
However, those arguments inside of scale_x_date don't seem to work anymore. Does anyone have a quick and easy solution?
如果我不提及 ggplot2文档这是一个很棒的资源,其中包含了大量有据可查的示例。当您不知道如何使用ggplot进行操作时,它们通常是最好的起点。
I would be remiss if I didn't mention the ggplot2 docs which are a fantastic resource, packed full of well-documented examples. They are often the best place to start when you don't know how to do something with ggplot.
您在当前框架中上面引用的代码的翻译是:
The "translation" for the code you quoted above in the current framework is:
scale_x_date(breaks='days', labels=date_format("%d%b"))
这将需要软件包 scales
。除非您的日期很少,否则您可能还需要进行
which will require the package scales
. Unless you have fairly few dates, you probably also would want an adjustment like
theme(axis.text.x = element_text(angle=45))
如果您只想标记您有数据,请尝试
If you only want to label the dates for which you have data, try
scale_x_date(breaks=df$date, labels = date_format("%m/%d"))
其中 df $ date
是您的数据框包含日期,当然您可以使用首选的日期格式字符串。
where df$date
is the column of your dataframe containing the dates, and of course you can use your preferred date format string.