簡體   English   中英

R:ggplot 在 x 軸上顯示所有日期

[英]R: ggplot display all dates on x axis

我有以下數據集

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")

我正在嘗試color by Wheel繪制Date vs WearRatecolor by Wheel 代碼如下:

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

它有效,但我想放置實際的日期標簽。 我該怎么做?

編輯

該圖目前看起來如此處所示。 但是,我想在 X 軸上看到“2015 年 8 月”、“2015 年 9 月”等,並且我想顯示所有刻度。

在此處輸入圖片說明

最簡單的方法是使用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 :沒有世紀的年份
有關完整可能性的描述,請參閱?strftime()

ggplot(data = df) + 
  geom_point(mapping = aes(x = Date, y = WearRate, color = Wheel))+
  scale_x_date(date_labels="%b %Y", breaks = unique(df$Date))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM