格式化ggplot2时间序列数据
Format ggplot2 time series data
我正在尝试在 gpplot 中生成一个整齐的线图
设置:
list1 <- c()
for (i in 1:100) {
list1[[i]]= paste("date",i, sep="_")
}
mat <- data.frame(matrix(nrow=100, ncol=2))
mat[,1] = list1
mat[,2] = rnorm(100,0,1)
colnames(mat) = c("date", "number")
ggplot(mat) + geom_line(aes(x=date, y=number, group=1)) +xlab("date") + ylab("number")
当我运行这段代码时,x轴无法解释。我该如何解决这个问题?
date
列必须是字符吗?如果您将其设置为实际日期,ggplot
会为您处理。
start = as.Date("2005-01-01")
end = start + 99
dates <- seq(from = start, to = end, by = "day")
mat <- NULL
mat$dates <- dates
mat$number = rnorm(100,0,1)
mat <- as.data.frame(mat)
ggplot(mat) +
geom_line(aes(x = dates, y=number, group=1)) +
xlab("date") +
ylab("number")
我正在尝试在 gpplot 中生成一个整齐的线图
设置:
list1 <- c()
for (i in 1:100) {
list1[[i]]= paste("date",i, sep="_")
}
mat <- data.frame(matrix(nrow=100, ncol=2))
mat[,1] = list1
mat[,2] = rnorm(100,0,1)
colnames(mat) = c("date", "number")
ggplot(mat) + geom_line(aes(x=date, y=number, group=1)) +xlab("date") + ylab("number")
当我运行这段代码时,x轴无法解释。我该如何解决这个问题?
date
列必须是字符吗?如果您将其设置为实际日期,ggplot
会为您处理。
start = as.Date("2005-01-01")
end = start + 99
dates <- seq(from = start, to = end, by = "day")
mat <- NULL
mat$dates <- dates
mat$number = rnorm(100,0,1)
mat <- as.data.frame(mat)
ggplot(mat) +
geom_line(aes(x = dates, y=number, group=1)) +
xlab("date") +
ylab("number")