IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

Is there a problem with the moderndive geom_parallel_lines function when there are facets? #8704

Open rdstern opened 8 months ago

rdstern commented 8 months ago

@fran2or I wonder if you or perhaps @jkmusyoka could check that I am not doing something silly here. I really like the geom_parallel_lines function, that is in the moderndive package.

It is working easily in the merged version and I would like to illustrate it for both survey and climatic data. Here it is for the survey data:

image

It gives the following graph:

image

Very nice. It is the model that the slope with fertiliser is the same for each variety. So (if true) then suggestions on fertiliser can be made separately from variety suggestions.

Here is the R code:

last_graph <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(x=fert, y=yield, colour=variety)) + ggplot2::geom_point() + moderndive::geom_parallel_slopes() + theme_grey()

Here it is for the 5 stations in Zambia Eastern Province:

image

So the model is of equal slopes (trends) for all 5 stations.

Here is the R code for this:

last_graph <- ggplot2::ggplot(data=eastern5_by_Station_Name_year, mapping=ggplot2::aes(x=year, y=mean_TMPMAX, colour=Station_Name)) + ggplot2::geom_point() + ggplot2::geom_line() + moderndive::geom_parallel_slopes() + theme_grey() + ggplot2::scale_fill_distiller(palette="Blues") Now, in either case, when we add facets, it appears to work, but the lines are no longer parallel!

So: image

from the R-code:

last_graph <- ggplot2::ggplot(data=survey, mapping=ggplot2::aes(x=fert, y=yield, colour=variety)) + ggplot2::geom_point() + moderndive::geom_parallel_slopes() + theme_grey() + ggplot2::scale_fill_distiller(palette="Blues") + ggplot2::facet_wrap(facets= ~ variety, dir="h")

And:

image

From R-code:

last_graph <- ggplot2::ggplot(data=eastern5_by_Station_Name_year, mapping=ggplot2::aes(x=year, y=mean_TMPMAX, colour=Station_Name)) + ggplot2::geom_point() + ggplot2::geom_line() + moderndive::geom_parallel_slopes() + theme_grey() + ggplot2::scale_fill_distiller(palette="Blues") + ggplot2::facet_wrap(facets= ~ Station_Name, dir="h")

I think this is a big fault, but am I doing something wrong here? If not I would like to report this on the moderndive site.

lilyclements commented 7 months ago

Thanks for this @rdstern. They have a warning come up in R (which you wouldn't see in R-Instat) when you run geom_parallel_slopes() with your facet example:

`geom_parallel_slopes()` didn't recieve a grouping variable with more than one unique value. Make sure you supply one. Basic model is fitted. 

I think what is happening is that they treat the different facets as different "plots". If I fit with Village as the facets instead, we can see that our lines are parallel within facets, but not between facets:

image

### R code:
ggplot2::ggplot(data=survey, mapping=ggplot2::aes(x=fert, y=yield, colour=variety)) +
  ggplot2::geom_point() + moderndive::geom_parallel_slopes() +
  theme_grey() + theme(legend.position = "none") +
  ggplot2::scale_fill_distiller(palette="Blues") +
  ggplot2::facet_wrap(facets= ~ village, dir="h") +
  labs(title = "geom_parallel_slopes function, with facets")

So it seems that this is intentional - they discuss this a bit here in their gist.

I'm sorry it's not quite the answer you wanted. I'm not sure how "easy" it is to make it so that we can have parallel lines between facets.

rdstern commented 7 months ago

@lilyclements many thanks for this. There seem to be 2 separate issues here. a) R-Instat doesn't currently display warnings. Why not? I think @lloyddewit mentioned this earleir. Would it be possible - perhaps even compulsory to display them. We do this when running code in R-Instat that can't be handled smoothly? Maybe it could get tedious in a session. I wonder if we could then switch it off. If so, then I would suggest that (to encouyrage good working practice) we always have warning switched on at the start of each R-Instat session. Then perhaps we could have an option to not display future warning in a given session, once we have each warning? b) Facets with parallel lines don't display well. I did like your example that we can use a different factor for the facets, e.g. village, and then lines for different varieties are parallel within each facet. But, given that warning, I have been playing with adding a group aesthetic to try to tempt the function to make the lines parallel over the facets? I didn't succeed. Do you think it is possible? I anyway need to change my issue on their site here. Unless you wanted to comment there?

lilyclements commented 7 months ago

@rdstern

a) my guess would be that the warnings might be a bit intimidating - warnings are common in R. For example, if you run a geom_histogram in R, you get a warning from the default bins argument. But perhaps if the user chooses to see the code in the right hand window, then it would be useful to have the warnings. At the very least, I agree that there should be an option there for a situation like this!

b) I tried to do a few bits to "make" it parallel between the facets but did not succeed either. I imagine it goes a bit deeper in the function - perhaps something to add to the issue on their site?

lilyclements commented 7 months ago

To add to a), I think if the user is writing R code in the script then it would be useful for them to see warnings. Warnings are useful.