Home > front end >  ggplotly showing numbers instead of date labels
ggplotly showing numbers instead of date labels

Time:12-03

I have a dataset with the following structure:

    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")

and I would like to plot a chart by date and other variables (e.g. mode of transport) in ggplotly. The chart is correct, but the labels that appear in the dates show numbers instead of date format. The variable in the database is in date format, and I already tried changing it to different formats and still does not work.

I would also like to add minor date breaks, but can't seem to get it right. Here is the code I am using for the chart:

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)

This is the chart plotted Chart

Any help would be greatly appreciated! :)

CodePudding user response:

Looks like the date class gets dropped when using stat="count". Hence, one option to achieve your desired result would be to aggregate your dataset before passing it to ggplot using e.g. 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()

enter image description here

A second option which additionally for setting the date format would be make use of the text "aesthetic" and convert your numbers back to proper dates:

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"))

enter image description here

  • Related