R:ggplot显示x轴上的所有日期 [英] R: ggplot display all dates on x axis

查看:2051
本文介绍了R:ggplot显示x轴上的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集:

I have the following data set

structure(list(Date = structure(c(16636, 16667, 16698, 16728, 
16759, 16789, 16820, 16851, 16880, 16911, 16636, 16667, 16698, 
16728, 16759, 16789, 16820, 16851, 16880, 16911, 16636, 16667, 
16698, 16728, 16759, 16789, 16820, 16851, 16880, 16911, 16636, 
16667, 16698, 16728, 16759, 16789, 16820, 16851, 16880, 16911, 
16636, 16667, 16698, 16728, 16759, 16789, 16820, 16851, 16880, 
16911), class = "Date"), Wheel = structure(c(5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L), .Label = c("L1", "L2", "L3", "L4", "L5", "L6", "R1", 
"R2", "R3", "R4", "R5", "R6"), class = "factor"), WearRate = c(-0.000367, 
0, 0, 0, 0.001888, 0, -0.00018, 0.000579, -0.000211, 0.000643, 
0.000106, 0, 0, 0, 0.000833, 0, -0.00036, 0.000811, -0.000819, 
0.002044, -0.00029, 0, 0, 0, 0.001666, 0, -0.000348, 0.000888, 
-0.000679, 0.001636, 8.7e-05, 0, 0, 0, 0.000666, 0, -0.000315, 
0.000618, -0.000585, 0.001636, -0.000512, 0, 0, 0, 0.002499, 
0, -0.000247, 0.000734, -9.4e-05, 0.000409)), .Names = c("Date", 
"Wheel", "WearRate"), row.names = 211269:211318, class = "data.frame")

我试图绘制 Date vs WearRate 按轮子颜色。代码如下:

I am trying to make a plot of Date vs WearRate and color by Wheel. The code is as follows:

ggplot(data = df) + geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))

它可以工作,但我想放置实际的日期标签。我该怎么做?

It works but I want to put actual date labels. How do I do it?

编辑

剧情目前看起来如图所示这里。然而,我想在X轴上看到2015年8月,2015年9月等,我想显示所有的蜱。

The plot currently looks as shown here. However, I want to see "Aug 2015", "Sep 2015" etc on X axis and I want to display all the ticks.

推荐答案

最简单的方法是使用 scale_x_date

ggplot(data = df) + 
  geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))+
  scale_x_date(date_labels="%b %y",date_breaks  ="1 month")

%b :简称月份名称

%y :年份without century

有关完整可能性的描述,请参见?strftime()

%b: Abbreviated month name
%y: Year without century
For a description of the full possibilities see ?strftime()

这篇关于R:ggplot显示x轴上的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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