【问题标题】:date on a ggplot graph not in a correct orderggplot 图上的日期顺序不正确
【发布时间】:2018-12-06 20:44:52
【问题描述】:

我是这个社区的新人,我已经开始使用 R 来完成我的大学学习。我在创建无法自己解决的图表时遇到问题。 我想创建一个折线图(x 轴:日期,y 轴:目击次数)。该图表未按正确顺序显示日期。它从 8 月 1 日开始,到 7 月 30 日结束。 Excel 文件中的日期已按正确顺序排列。 我试过这个:

number_sigthings$date = as.Date(number_sightings$date, format = "%d.%m%.%y")
number_sightings$date <- factor(number_sightings$date, ordered = T)

但两者都无助于解决问题。

如果有人可以帮助我,我将非常高兴。非常感谢。

这是我写的脚本:

options(stringsAsFactors = FALSE)

input1 <- "C:\\Users\\Hannah\\Documents\\Meeresbiologie\\Projekt Stella\\Daten Dänemark\\Dänemark Theodolitmessungen\\number_sightings.csv"

number_sightings <- read.csv(input1, sep=";")
library (lubridate)
library(ggplot2)

number_sigthings$date = as.Date(number_sightings$date, format = "%d.%m%.%y")

number_sightings$date <- factor(number_sightings$date, ordered = T)
plot <-ggplot(number_sightings, aes(x=date, y=number, group=1)) + geom_point(stat="identity") + geom_line(linetype="dashed")

plot <- plot +  theme(panel.background = element_rect(fill = "white"))
plot <- plot + theme(panel.grid.minor=element_blank())
plot <- plot + theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank())
plot <- plot + scale_x_date(breaks=seq("30.07.18, 02.09.18"),expand=c(0,0), labels=date_format("%d.%m.%y"), date_breaks = "1 day", date_minor_breaks = "1 day")
plot <- plot+ggtitle("")+(ylab("number of harbour porpoise sightings/hour"))
print(plot)

我在输入 csv 文件后在脚本中写了这个。

dput(number_sightings) 结构(列表(日期 = c(“31.07.2018”,“01.08.2018”,“02.08.2018”, “03.08.2018”、“06.08.2018”、“07.08.2018”、“08.08.2018”、“09.08.2018”、 “13.08.2018”、“15.08.2018”、“17.08.2018”、“22.08.2018”、“23.08.2018”、 "24.08.2018", "25.08.2018"), 数字 = c(2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25, 2, 1.88, 2.67, 3.04)), class= "data.frame", row.names = c(NA, -15L))

【问题讨论】:

  • 如果你提供数据会很有帮助,在你读入数据后使用dput(number_sightings)
  • 您不需要这一行:factor(number_sightings$date, ordered = T),一旦将列转换为 Date 对象,将其保留为 Date 对象。请参阅上面关于提供数据样本的评论。
  • 感谢您的回答。我在脚本中编写了 dput(number_sightings) ,它以正确的顺序向我显示日期和数字,但图表没有更改日期。你有其他想法我能做什么吗?
  • 在上面编辑您的帖子并包含dput(number_sightings)dput(head(number_sightings)) 的输出。
  • 好的,谢谢。第一次阅读后我误解了它,但现在我明白了,我编辑了我的帖子。

标签: r ggplot2


【解决方案1】:

在从字符转换为日期时,需要确保格式选项与文本格式匹配。

library(ggplot2)
#test data
number_sightings<-structure(list(date = c("31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018", "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018", "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018", "23.08.2018", "24.08.2018", "25.08.2018"), number = c(2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25, 2, 1.88, 2.67, 3.04)), class = "data.frame", row.names = c(NA, -15L))

#convert from character to Date Object (notice the format string)
number_sightings$date = as.Date(number_sightings$date, format = "%d.%m.%Y")

#number_sightings$date <- factor(number_sightings$date, ordered = T)
plot <-ggplot(number_sightings, aes(x=date, y=number, group=1)) + geom_point(stat="identity") + geom_line(linetype="dashed")

plot <- plot +  theme(panel.background = element_rect(fill = "white"))
plot <- plot + theme(panel.grid.minor=element_blank())
plot <- plot + theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank())

#Corrected this line (%y for 2 digit year, %Y or 4 digit year)
plot <- plot + scale_x_date(expand=c(0,0), date_labels=("%d.%m.%y"), date_breaks = "4 day")
plot <- plot+ggtitle("")+(ylab("number of harbour porpoise sightings/hour"))
print(plot)

【讨论】:

    【解决方案2】:
    library(ggplot2)
    #test data
    number_sightings<-structure(list(date = c("31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018", "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018", "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018", "23.08.2018", "24.08.2018", "25.08.2018"), number = c(2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25, 2, 1.88, 2.67, 3.04)), class = "data.frame", row.names = c(NA, -15L))
    
    #convert from character to Date Object (notice the format string)
    number_sightings$date = as.Date(number_sightings$date, format = "%d.%m.%Y")
    
    #number_sightings$date <- factor(number_sightings$date, ordered = T)
    plot <-ggplot(number_sightings, aes(x=date, y=number, group=1)) + geom_point(stat="identity") + geom_line(linetype="dashed")
    
    plot <- plot +  theme(panel.background = element_rect(fill = "white"))
    plot <- plot + theme(panel.grid.minor=element_blank())
    plot <- plot + theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank())
    
    #Corrected this line (%y for 2 digit year, %Y or 4 digit year)
    plot <- plot + scale_x_date(expand=c(0,0), date_labels=("%d.%m.%y"), date_breaks = "4 day")
    plot <- plot+ggtitle("")+(ylab("number of harbour porpoise sightings/hour"))
    print(plot)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多