隐藏x轴ggplot2中缺少的日期 [英] hide missing dates from x-axis ggplot2

查看:220
本文介绍了隐藏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.

这篇关于隐藏x轴ggplot2中缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆