tidyLPA
tidyLPA copied to clipboard
Variables ordered incorrectly with plot_bivariate
I haven't seen any other ticket opened for this issue, so I'm not entirely sure if it is related to something I did wrong.
These are my inputs:
iris_profiles<-iris %>%
select(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) %>%
estimate_profiles(n_profiles = 3) %>%
plot_bivariate()
And my output is the following:
As you can see the variables repeat itself and are incorrectly ordered.
I have tracked down the error to line 37 of R/plot-bivariate.R:
cors <- apply(expand.grid(vars, vars), 1, paste0, collapse = ".WITH.")[which(upper.tri(diag(vars)))]
The value of cors
is the following:
[1] "Sepal.Length.WITH.Sepal.Width"
[2] "Sepal.Length.WITH.Petal.Length"
[3] "Sepal.Width.WITH.Petal.Length"
[4] "Sepal.Length.WITH.Petal.Width"
[5] "Sepal.Width.WITH.Petal.Width"
[6] "Petal.Length.WITH.Petal.Width"
If you watch closely the rows are not extracted in the correct order, in this case "Sepal.Length.WITH.Petal.Width" is in position 4 when it should be in position 3. That is a problem because when plot_bivariate()
creates the cor_plotlist
in [line 183 of R/plot-bivariate.R](https://github.com/data-edu/tidyLPA/blob/master/R/plot-bivariate.R#L183:
cor_plotlist <- lapply(unique(df_plot$Parameter), function(this_cor){...}
It makes cor_plotlist
follow the same order of variable cors
, incorrectly ordering the variables and individual plots, resulting in a grid plot like the one shown above.
I made an adjustment to the line 37 of R/plot-bivariate.R:
cors <- matrix(apply(expand.grid(vars, vars), 1, paste0, collapse = ".WITH."), nrow=length(vars), byrow=TRUE)[which(lower.tri(diag(vars)))]
I don't know if it is the best approach. But my understanding of what R is doing is that it is selecting the matrix elements by columns and not by rows. This way I just transpose the matrix.
Hi
It looks like the issue that raised above has not been resolved yet. Could you check it for me?
Best Regards Seol