njudd / ggrain

{package} Make beautiful Raincloud plots in R!
Other
67 stars 4 forks source link

Problem conecting lines in rain.side = 'f2x2' #6

Closed franfrutos closed 1 year ago

franfrutos commented 1 year ago

Hi! I wanted to report an error when trying to connect the dots of different conditions when rain.side = 'f2x2'. I followed your vignette explaining how to do it, but I don't know why it looks like the lines connect the points within each category on the x-axis.

Here is a reproducible example:

library(ggrain)

# Creating fake data:
fake_d <- expand.grid(list(ID = seq(50), Cond_1 = c("A", "B"), Cond_2 =  c("C", "D")))
fake_d$RESP <- rbinom(nrow(fake_d), size = 50, prob = .5)/50

# Raincloud Plot:
  ggplot(fake_d, aes(Cond_1, RESP, fill = Cond_2, color = Cond_2)) +
  geom_rain(alpha = .5, rain.side = 'f2x2', id.long.var = "ID") + 
  theme_classic() +
  scale_fill_manual(values=c("dodgerblue", "darkorange")) +
  scale_color_manual(values=c("dodgerblue", "darkorange")) +
  guides(color = "none", fill = "none")

And here is the output of the above code:

rain_ex

I don't know exactly where the problem is, since the data structure should be correct.

Thanks you in advance!

njudd commented 1 year ago

Hey, I have actually come across this issue before! Thanks for the reproEx.

It is because the data structure is incorrect, so you have the same subject id "ID" for subjects with (cond_1 A + cond_2 C) & (cond_1 A + cond_2 D).

You can see this for ID 1: fake_d[fake_d$ID==1,]

So the behavior id.long.var = "ID" is as intended; it is connecting the lines by the ID, but obviously it looks wrong. It is connecting a line for subject 1 for all those points (including within cond_1). If you really want it to not have that behavior, you can redo the ID's, so they're unique subjects within cond_1 A & cond_1 B.

Yet, I personally recommend against that - people will look at a 2-by-2 plot and assume it is two groups (of different individuals). Instead, I would recommend making two 1-by-1 rain clouds and adding them together with patchwork. This will make it clearer to the reader that it is two different things/phenomena pre/post within the same individual.

franfrutos commented 1 year ago

Thank you for your response! You are right. Since when you summarize data using dplyr it gives you a format similar to the example above, I usually deal with that problem from ggplot2 itself. For example, using aes(group = interaction(ID, Cond_2) in the above example.

To make it work with ggrain there would be an extra step to do when summarizing the data:

fake_d$ID <- paste0(fake_d$ID, fake_d$Cond_2)

In my case this does the trick, following your suggestions.

Regarding the last thing you comment about 2 x 2 raincloudplots, although it is simple to create two plots and then join them using patchwork, I was wondering if something like facet_wrap(.~Cond_2) would be possible. I have tried, but it seems that the boxplot and violin misalign their position when you try to create facets. I attach an image of what it would look like:

plot_facet

I tried changing the positions manually as you suggest in the vignette, but it didn't come out very well. Is there any way to adjust the positions when using this strategy?

njudd commented 1 year ago

Manual adjustment should work. I would take the strategy of trying it for 1 plot subsetted and then faceting. Alternatively, maybe rain.side = 'f2x2' works in this situation?