我有一個具有以下結構的資料集:
structure(list(mes = c(7, 7, 7, 4, 4), ano = c(2021, 2021, 2021,
2021, 2021), nacionalidad = c("Venezuela", "Venezuela", "Venezuela",
"Venezuela", "Venezuela"), centro = c("Aeropuerto Eldorado",
"Aeropuerto Eldorado", "Aeropuerto Eldorado", "Aeropuerto Eldorado",
"Aeropuerto Eldorado"), puesto = c("Aeropuerto Eldorado de Bogotá",
"Aeropuerto Eldorado de Bogotá", "Aeropuerto Eldorado de Bogotá",
"Aeropuerto Eldorado de Bogotá", "Aeropuerto Eldorado de Bogotá"
), transporte = c("Air", "Air", "Air", "Air", "Air"), ciudad = c("Arauca",
"Bogotá", "Pereira", "Bogotá", "Bogotá"), flujo = c("Entries",
"Entries", "Entries", "Entries", "Entries"), motivo = c("Tourism",
"Tourism", "Tourism", "Transit", "Transit"), edad = c("0-17",
"0-17", "0-17", "18-29", "18-29"), colombiano = c("Extranjeros",
"Extranjeros", "Extranjeros", "Extranjeros", "Extranjeros"),
departamento = c("Arauca", "Bogota D.C.", "Risaralda", "Bogota D.C.",
"Bogota D.C."), region = c("América del Sur", "América del Sur",
"América del Sur", "América del Sur", "América del Sur"
), status = c("Permiso de Turismo", "Permiso de Turismo",
"Permiso de Turismo", "Permiso Otras Actividades", "Permiso Otras Actividades"
), departamento_2 = c("Bogotá", "Bogotá", "Bogotá", "Bogotá",
"Bogotá"), destino_procedencia = c("Emiratos árabes", "Israel",
"Emiratos árabes", "Panamá", "Panamá"), region_destino = c("Asia",
"Asia", "Asia", "América Central y el Caribe", "América Central y el Caribe"
), sexo = c("Male", "Male", "Male", "Male", "Male"), numero = c(1,
1, 1, 5, 5), date = structure(c(18809, 18809, 18809, 18718,
18718), class = "Date"), date2 = structure(c(18809, 18809,
18809, 18718, 18718), class = "Date")), row.names = c(NA,
5L), class = "data.frame")
我想在 ggplotly 中按日期和其他變數(例如運輸方式)繪制圖表。圖表是正確的,但日期中出現的標簽顯示的是數字而不是日期格式。資料庫中的變數是日期格式,我已經嘗試將其更改為不同的格式,但仍然不起作用。
我還想添加一些小日期休息時間,但似乎做對了。這是我用于圖表的代碼:
chart9<-ggplot()
geom_line(data=Flow,
aes(x=date,
color=transporte), stat="count")
scale_x_date(date_minor_breaks = "1 month",
date_labels = "%Y (%b)")
labs(color="Type of Flow")
ggtitle("Number of Entrances, by Month and Mode of Transportation, 2017-2021")
xlab("Date")
ylab("Number or People")
ggplotly(chart9)
這是繪制的圖表 
任何幫助將不勝感激!:)
uj5u.com熱心網友回復:
看起來日期類在使用stat="count". 因此,實作您想要的結果的一種選擇是在將資料集傳遞給 ggplot 之前聚合您的資料集,例如dplyr::count(Flow, date, transporte):
Flow <- dplyr::count(Flow, date, transporte, name = "count")
ggplot()
geom_line(data = Flow, aes(x = date, y = count, color = transporte))
scale_x_date(
date_minor_breaks = "1 month",
date_labels = "%Y (%b)"
)
labs(color = "Type of Flow")
ggtitle("Number of Entrances, by Month and Mode of Transportation, 2017-2021")
xlab("Date")
ylab("Number or People")
ggplotly()

另外用于設定日期格式的第二個選項是利用text“美學”并將您??的數字轉換回正確的日期:
library(plotly)
ggplot()
geom_line(data = Flow, aes(x = date, color = transporte, text = paste(
"count:", ..count..,
"<br>Date: ", format(as.Date(..x.., origin = "1970-01-01"), "%Y (%b)"),
"<br>transporte: ", ..color..
)), stat = "count")
scale_x_date(
date_minor_breaks = "1 month",
date_labels = "%Y (%b)"
)
labs(color = "Type of Flow")
ggtitle("Number of Entrances, by Month and Mode of Transportation, 2017-2021")
xlab("Date")
ylab("Number or People")
ggplotly(tooltip = c("text"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371918.html
