簡體   English   中英

R 工作室 ggplot2 geom_point X 軸未正確顯示點

[英]R studio ggplot2 geom_point X axis not showing points properly

ggplot(DelaysTimeofDayTotal, aes(CRSDepTime)) + geom_point(aes(y = DepDelay, color = "DepDelay"))
+ geom_point(aes(y=ArrDelay, color = "ArrDelay")) 
+ scale_x_discrete(limits=(day.name)) 
+ labs(x= "Time of Day", y = "Delays in minutes") 
+ ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") 
+ theme(plot.title = element_text(face="bold"))

這是output

在此處輸入圖像描述

這是 dataframe 使用 (int) (num) (num) CRSDepTime 整數表示 HHMM

在此處輸入圖像描述

我是 R 的新手,非常感謝您的幫助。

您應該將day.name定義為因子變量並使用breaks()使 x 軸可讀。

示例代碼:

library(ggplot2)

DelaysTimeofDayTotal$day.name= factor(DelaysTimeofDayTotal$day.name, levels= c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))

ggplot(DelaysTimeofDayTotal, aes(day.name, CRSDepTime)) + 
  geom_point(aes(y = DepDelay, color = "DepDelay"))+
  geom_point(aes(y=ArrDelay, color = "ArrDelay")) +
  scale_x_discrete(breaks=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) +
 labs(x= "Time of Day", y = "Delays in minutes", color="Delay Status") +
 ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") +
 theme(plot.title = element_text(face="bold"))+
  theme_minimal()

Plot:

在此處輸入圖像描述

另外,我寧願使用geom_jitter()來可視化重疊點。

示例代碼:

ggplot(DelaysTimeofDayTotal, aes(day.name, CRSDepTime)) + 
  geom_jitter(aes(y = DepDelay, color = "DepDelay"))+
  geom_jitter(aes(y=ArrDelay, color = "ArrDelay")) +
  scale_x_discrete(breaks=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) +
 labs(x= "Time of Day", y = "Delays in minutes", color="Delay Satus") +
 ggtitle(" Year 2005 - 2007 Average Delay for Time of Day") +
 theme(plot.title = element_text(face="bold"))+
  theme_minimal()

Plot:

在此處輸入圖像描述

樣本數據:

DelaysTimeofDayTotal<-structure(list(CRSDepTime = c(510, 511, 512, 514, 515, 516, 517, 
518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530
), DepDelay = c(-0.4529247, 4.4774775, -2.5608586, 1.9598214, 
-0.32222292, -3.86666667, 4.91714286, -4.1439271, 6.2290914, 
0.9113794, 2.4074074, 0.1282051, 1.1053494, 0.8019896, 0.69339187, 
-2, 2.1190476, 2.74566427, 4.2522819, 1.0220646), ArrDelay = c(-1.34935901, 
1.14414414, -0.67111557, -1.62053571, -2.6085016, -11.8666667, 
9.4, -4.131957, 7.10905807, -0.57737008, 0.38888889, 1.10256441, 
10.7990334, -5.47912288, -0.75862405, 8, -9.6190476, 4.943389978, 
7.19421907, -0.88228056), day.name = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 
6L), .Label = c("Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Saturday", "Sunday"), class = "factor")), spec = structure(list(
    cols = list(CRSDepTime = structure(list(), class = c("collector_double", 
    "collector")), DepDelay = structure(list(), class = c("collector_double", 
    "collector")), ArrDelay = structure(list(), class = c("collector_double", 
    "collector")), day.name = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x1ac39d58>, row.names = c(NA, 
-20L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))

暫無
暫無

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

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