【发布时间】:2022-01-14 06:00:01
【问题描述】:
我需要您的帮助来查找此代码中的错误。我收到以下错误消息:Error: Invalid input: date_trans works with objects of class Date only。我认为问题出在scale_x_datearguments,但我无法解决。谢谢。
library(ggplot2)
library(scales)
library(lubridate)
library(readxl)
entrada<- read_excel("R_codes_examples/entrada_turistas.xlsx", sheet = "mensal", col_types =
c("date", "numeric"))
ggplot(entrada, aes(x = entrada$`mes_ano`, y = entrada$`movimento_de_passageiros`)) +
geom_line( colour = "#0c4c8a") +
scale_x_date(date_breaks = "6 months",labels = date_format("%b-%Y"),limits = c(as.Date("2006-
08-01"), NA)) +
scale_y_continuous(labels=function(n){format(n, scientific = FALSE)}) +
labs(y= "Movimento de Passageiros mensais 2006 a 2017 ", x = "Mês/Ano") +
xlab("") +
theme(axis.text.x=element_text(angle=60, hjust=1))
请在下面找到我的代码头:
> dput(head(entrada))
structure(list(mes_ano = structure(c(1136073600, 1138752000,
1141171200, 1143849600, 1146441600, 1149120000), tzone = "UTC",
class = c("POSIXct", "POSIXt")), movimento_de_passageiros =
c(119764, 100442, 114198,
124676, 113431, 115482)), row.names = c(NA, -6L), class =
c("tbl_df", "tbl", "data.frame"))
【问题讨论】:
-
查看
entrada$`mes_ano`的类。正如错误消息告诉您的那样:scale_x_date仅适用于 Date 类的对象。顺便说一句:摆脱entrada$。只需使用aes(x = `mes_ano`, ...)。 -
尝试entrada
-
我们只能推测而不向我们展示您的(部分)数据。您的日期很可能存储为字符而不是日期类。尝试向我们提供一个 dput(head(entrada)) 以便我们可以实际帮助您。
-
@KeltonSantos 我会用 geom_col() 替换 geom_line(color = "#0c4c8a")
-
@Rfanatic 我试过了,但我遇到了同样的错误。谢谢。