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