Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables ordered incorrectly with plot_bivariate #198

Open
leosfsm opened this issue Nov 22, 2022 · 1 comment
Open

Variables ordered incorrectly with plot_bivariate #198

leosfsm opened this issue Nov 22, 2022 · 1 comment

Comments

@leosfsm
Copy link

leosfsm commented Nov 22, 2022

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:
bivariate_error

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.

@hyunsooseol
Copy link

Hi

It looks like the issue that raised above has not been resolved yet. Could you check it for me?

Best Regards
Seol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants