我是这个社区的新成员,我已经开始与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)),类=“ data.frame”,row.names = c(NA, -15L))
答案 0 :(得分:0)
从字符转换为日期时,需要确保格式选项与文本格式匹配。
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)
答案 1 :(得分:0)
write.tucson (dat, "tucson.txt", prec = 0.01, long.names = TRUE)