ggplotly dates show as 19 thousand series of numbers

Below is the code to generate a bar graph from a csv file that has the dates in y-m-d format.

library(tidyverse)
library("lubridate")
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(htmlwidgets)
library("reprex")


turtle_activity_gtm <-  read_csv("https://www.dropbox.com/s/7ubvhajvkhx53kc/mpt_act_rep_cum_fc_cum_nest.csv?dl=1")
#> Rows: 765 Columns: 66
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (22): beach, county, activity, ref_no, activity_comments, encountered?,...
#> dbl  (35): uid, activity_no, fcrawl, fcrawl_cum, clutch, clutch_cum, nest_no...
#> lgl   (6): final_treatment, light_management, relocation_reason, lost_nest, ...
#> date  (3): activity_date, emerge_date, inventory_date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Above file has cumulative false crawls and cumulative nests added along with proper date formats.

turtle_activity_gtm$a_date <- turtle_activity_gtm$activity_date


head(turtle_activity_gtm$activity_date,20 )
#>  [1] NA           "2022-04-19" "2022-05-01" "2022-05-01" "2022-05-06"
#>  [6] "2022-05-06" "2022-05-07" "2022-05-07" "2022-05-09" "2022-05-09"
#> [11] "2022-05-10" "2022-05-13" "2022-05-14" "2022-05-14" "2022-05-14"
#> [16] "2022-05-15" "2022-05-15" "2022-05-15" "2022-05-15" "2022-05-15"

start_date <- ymd("2022-04-15")
end_date <- ymd("2022-10-31")

ggstatic <-  ggplot(data = (turtle_activity_gtm |> filter(activity=="N"))) +
  
  geom_bar(aes(x = activity_date, fill = species),
           na.rm = TRUE,
           position = position_dodge2(preserve = "single")) +
  scale_x_date(breaks = c(seq.Date((start_date), (end_date), 
                                   by = '10 days'), end_date), 
               date_labels = "%m/%d",
               date_minor_breaks = "1 day",
               limits = c( start_date, end_date),
               expand = c(0,0)) 

ggstatic

Created on 2023-02-02 with reprex v2.0.2
When I was first learning how to use ggplot with dates I did come across dates as 19 thousand numbers. But I quickly figured out how to handle dates and kind of forget about this date format. Now it has returned.

ggplotly(ggstatic)

When I run the ggplotly of this graph, holding the cursor over a bar shows the dates as 19xxx.00. For example, in screen capture I took shows 2022-06-29 as 19172.00. Is there a way I can get a more readable date format to show up?

Thanks,
Jeff

I did some more searching and found out where these numbers come from: days since 1/1/1970.

unclass(as.Date("2022-06-29")) #= 19172

Nevertheless, how can I get those dates show up as some common date format?

The date values displayed as a tool tip are formatted as %Y-%m-%d if I summarize the data before calling ggplot() and I use geom_col()

ggstatic <-  turtle_activity_gtm |> filter(activity=="N") |>
  group_by(activity_date, species) |> 
  summarize(N = n()) |> 
  ggplot() +
  geom_col(aes(x = activity_date, y = N, fill = species),
           na.rm = TRUE,
           position = position_dodge2(preserve = "single")) +
  scale_x_date(breaks = c(seq.Date((start_date), (end_date), 
                                   by = '10 days'), end_date), 
               date_labels = "%m/%d",
               date_minor_breaks = "1 day",
               limits = c( start_date, end_date),
               expand = c(0,0)) 
plotly::ggplotly(ggstatic)
1 Like

Thanks.

In the interim, I had found out about using tooltip. And found I could achieve similar results using

ggstatic <-  ggplot(data = (turtle_activity_gtm |> filter(activity=="N"))) +
  
  geom_bar(aes(x = activity_date, fill = species,
               text = paste('Date; ', as.Date(activity_date), 
                            '<br>Species: ', species
                            
               )),
           na.rm = TRUE,
           position = position_dodge2(preserve = "single")) +
  scale_x_date(breaks = c(seq.Date((start_date), (end_date), 
                                   by = '10 days'), end_date), 
               date_labels = "%m/%d",
               date_minor_breaks = "1 day",
               limits = c( start_date, end_date),
               expand = c(0,0)) 


plotly::ggplotly(ggstatic, tooltip = c("count", "text"))

Thanks again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.