I am following the instructions with my df and I am trying this code but it doesnt work.
df %>%
ggplot(aes(x = tipo_fecha, y = fecha)) +
stat_halfeye()
But the answer is this
Warning message:
Computation failed in `stat_slabinterval()`.
Caused by error in `trans$transform()`:
! `transform_date()` works with objects of class <Date> only
Hi, thanks for the answer.
The first code works in a certain way because now the x-axis is with numbers instead of dates and it is not usefull for me.
This is the output on the first code. (I used coord_flip() on the code)
I don't know why stat_halfeye is failing to process your dates correctly. The plot you got after using as.numeric shows that your dates are truly numeric dates. I reproduced your error by inventing some data with dates as the continuous variable. The only way I can get the plot to work is to use the numeric values of the dates for the plot and then label the axis with the corresponding dates. Here is an example of that. In my data, the fecha column starts as integers. You will have to convert yours using the mutate() and as.numeric() functions as in my previous post.
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.3
library(ggdist)
#> Warning: package 'ggdist' was built under R version 4.3.3
set.seed(123)
df <- tibble(fecha = as.integer(rnorm(1000, mean = 18000, sd = 400)),
tipo_fecha = sample(c("A","B","C","D"), 1000, replace = TRUE))
Dates = c("2015-01-01", "2016-01-01","2017-01-01","2018-01-01",
"2019-01-01","2020-01-01","2021-01-01","2022-01-01",
"2023-01-01","2024-01-01")
NumericDates <- as.Date(Dates)
df %>%
ggplot(aes(y = tipo_fecha, x = fecha)) +
stat_halfeye() +
scale_x_continuous(breaks = NumericDates, labels = Dates)