Why my code for raincloud plot doesnt works

I have this df

# A tibble: 24.727 × 2
   tipo_fecha      fecha     
   <fct>           <date>    
 1 fecha_informe_1 2015-08-04
 2 fecha_informe_2 2021-06-11
 3 fecha_informe_3 2022-12-14
 4 fecha_informe_1 2015-08-11
 5 fecha_informe_2 2021-05-12
 6 fecha_informe_1 2015-08-11
 7 fecha_informe_2 2021-03-22
 8 fecha_informe_1 2015-08-05
 9 fecha_informe_2 2016-09-19
10 fecha_informe_1 2015-08-11
# ℹ 24.717 more rows
# ℹ Use `print(n = ...)` to see more rows

And I am exploring the raincloud plot

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

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 

and the output is this plot

I don't see anything wrong with your code. What happens if you try

df %>% 
  mutate(fecha = as.numeric(fecha)) %>%
  ggplot(aes(x = tipo_fecha, y = fecha)) + 

If that works, something very strange is happening with stat_halfeye.
Also, please try this

df %>% 
  ggplot(aes(x = tipo_fecha, y = fecha)) + 
  stat_halfeye(.width = 0)

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 am exploring different types of graphics but just so you understand what I want to do this is my benchmark.

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.

#> Warning: package 'ggplot2' was built under R version 4.3.3
#> Warning: package 'ggdist' was built under R version 4.3.3
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",
NumericDates <- as.Date(Dates)
df %>% 
  ggplot(aes(y = tipo_fecha, x = fecha)) + 
  stat_halfeye() +
  scale_x_continuous(breaks = NumericDates, labels = Dates)

Created on 2024-04-22 with reprex v2.0.2

Hi, thanks for the answer.
It works! It's not the best solution but at least it's something.

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.