误差在ggplot就R(Error in ggplot In R)

2019-10-17 12:13发布

我想用ggplot绘制R中的时间序列数据,但我已经碰到这样的错误:

Error in continuous_scale(aesthetics, "date", identity, breaks = breaks,  : 
unused argument(s) (format = "%b-%Y")

句法:

ggplot(p29, aes(dt, ambtemp)) + geom_line() +
    scale_x_date(format = "%b-%Y") + xlab("") + ylab("Tempreture")

文件名: p29

  ambtemp                  dt surtemp
1   -1.14 2007-09-29 00:01:57  -2.712
2   -1.12 2007-09-29 00:03:57  -2.775
3   -1.33 2007-09-29 00:05:57  -2.712
4   -1.44 2007-09-29 00:07:57  -2.837
5   -1.54 2007-09-29 00:09:57  -2.775
6   -1.29 2007-09-29 00:11:57  -2.900

Answer 1:

在功能scale_x_date()你应该写labels=date_format("%b-%Y") 也列dt格式应为Date

library(scales)
p29$dt=as.Date(p29$dt, format="%Y-%m-%d %H:%M:%S")    
ggplot(p29, aes(dt, ambtemp)) + geom_line() +
scale_x_date(labels=date_format ("%b-%Y")) + xlab("") + ylab("Tempreture")  

要显示小时和分钟,而不是函数scale_x_date()你应该使用scale_x_datetime()和写入labels=date_format("%H:%M") 为了控制符参数breaks=date_breaks()被使用。 也列dt格式应为POSIXt

library(scales)
p29$dt=strptime(p29$dt, "%Y-%m-%d %H:%M:%S")
ggplot(p29, aes(dt, ambtemp)) + geom_line() +
scale_x_datetime(breaks = date_breaks("5 min"),labels=date_format("%H:%M")) + xlab("") + ylab("Tempreture")


文章来源: Error in ggplot In R