X轴日期不按时间顺序排列

X axis dates not in chronological order

我是 Rstudio 的新手,所以我搜索了相关问题等,但我仍然无法正确组织图表。我无法让图表按正确的时间顺序显示日期。想知道我是否可以找人看看我的代码和数据,看看我做错了什么(请非常简单地解释,我是新手)。

我目前正在读取一个 CSV 文件,它是这样设置的 1:

AdD = 采样日期,AdT = 采样时间,AdV = 浓度值 - 这些是水样,两个月中仅包含这两个样本(每月一个)

我得到了图表:

图表在 x 轴上首先显示第 5 个月,当我希望它按时间顺序(又名 4 月 - 第 4 个月)首先出现时。

我的代码如下(请忽略 geom_hline 和空白的轴元素 - 这是 facet 中的 6 个图表之一,与我 think/hope 的日期问题无关) :

F1ambH <- read_csv("data 1 Amb.csv")

f1ambH <- ggplot(data=F1ambH, aes(x=AhD, y=AhV))+  geom_point() +theme_bw()+labs(y= "Concentration (µg/L)", x = "Sample Date") 

f1ambH <- f1ambH + geom_hline(yintercept=1.1, linetype="dashed", color="steelblue")+ theme(axis.title.x = element_blank())+ theme(axis.title.y = element_text(face = "bold", size = 11))

f1ambH

我也试过像这样改变数据:

F1ambH <- read_csv("data 1 Amb.csv") %>% mutate(dates = dmy(AhD))

f1ambH <- ggplot(data=dates, aes(x=AhD, y=AhV))+  geom_point() +theme_bw()+labs(y= "Concentration (µg/L)", x = "Sample Date") 

生成此图:

它正确显示了日期,但图表上的两个点没有我需要的相应 x 轴刻度(我觉得我已经用尽了我的选择来尝试修复

所以,如果我能解决任何一个问题,那就太棒了。

美国东部时间: 按照第一条评论的建议使用 +scale_x_date(breaks=unique(F1ambH$dates)) 似乎可以解决我的问题,但这些点现在位于图表的另一侧并且看起来很可怕,有没有办法清理它? Figure

使用你的第二个解决方案,但使用

+scale_x_date(breaks=unique(dates))

指定你想要断点的位置。

如果将 x 变量作为因子并将其添加到绘图之前,它会保持顺序:

F1ambH$AhD <- factor(F1ambH$AhD,levels=unique(F1ambH$AhD),order=TRUE)

f1ambH <- ggplot(data=F1ambH, aes(x=AhD, y=AhV))+  geom_point() +theme_bw()+labs(y= 
         "Concentration (µg/L)", x = "Sample Date") 

f1ambH <- f1ambH + geom_hline(yintercept=1.1, linetype="dashed", color="steelblue")+ 
       theme(axis.title.x = element_blank())+ theme(axis.title.y = element_text(face = 
            "bold", size = 11))

f1ambH

甚至你可以有任何你喜欢的顺序:

F1ambH$AhD <- factor(F1ambH$AhD,levels=c(your preference order),order=TRUE)