jaredlander / coefplot

Plotting model coefficients
Other
27 stars 19 forks source link

Sort not working in multiplot when using 'by = 'model'' #24

Open victorhartman opened 2 years ago

victorhartman commented 2 years ago

The coefplot package has been very useful in creating graphs that easily and clearly explain results. Thank you for that!

Now I am trying to compare coefficients between different models, in effect comparing countries. However I am having a hard time getting the sort option to work. It does not seem to do anything when using 'by = 'model''. Also is it possible to reverse the sort order?

My code looks like this. I have several logit models with all the same coefficients.

coefplot::multiplot(`logit_model_Belize_2015-2016`,
                    `logit_model_Costa Rica_2011`,
                    logit_model_Cuba_2019,
                    `logit_model_Dominican Republic_2014`,
                    logit_model_Guyana_2014,
                    logit_model_Mexico_2015,
                    logit_model_Panama_2013,
                    logit_model_Suriname_2018,
                    `logit_model_Trinidad and Tobago_2011`,
                    `logit_model_Uruguay_2012-13`,
                    coefficients = "weighted_excluded",
                    names = c("Belize",
                              "Costa Rica",
                              "Cuba",
                              "Dominican Republic",
                              "Guyana",
                              "Mexico",
                              "Panama",
                              "Suriname",
                              "Trinidad",
                              "Uruguay"),
                    sort = 'magnitude',
                    by = "Model")
jaredlander commented 2 years ago

Sorting the coefficients with multiplot() is tough. What do you do when for model 1 the order should coefficients a,c,b and for model 2 the coefficients should be a,b,c? By the nature of {ggplot2} all the points for a have to be together, and for b, etc.

If this were a tabular display you could have a two-stage sort, first by model then by coefficient. I'm not sure how to implement this for plots though.

If you have any ideas I'm happy to see what I can do.

victorhartman commented 2 years ago

Thanks for the response. I understand the difficulty of making this possible. My knowledge on R is however limited, so I don't think I can be of much help.

Is it however not easier in my case when I only select one coefficient (all my models are also exactly the same)?

My original issue, which is btw much easier, was that the names I provided did not sort in ascending order. I fixed this by adding + scale_y_discrete(limits = rev) at the end.