1

After running my main model (call it main_model), I use the function margins() (to get the average marginal effects) and then run summary() (to get SE, Z-score, P-values and the confidence intervals around the average margins). In order to plot the marginal effect, I use the ggplot function.

The problems are:

  1. summary() re-orders the independent variables alphabetically [I don’t know why], and;
  2. In the plot produced, the independent variables are ordered alphabetically but in reverse order.

Is it possible to change the order of the independent variables in the y-axis of the plot produced? For example, can I choose the order manually? Or could I, at least, have the alphabetical order in ascending order?

Here is my code so far:

marg.pol2 = margins(main_model) 
marg.pol2 = summary(marg.pol2)

plot_ME <- ggplot(data = marg.pol2) +
geom_point(aes(factor, AME)) +
geom_errorbar(aes(x = factor, ymin = lower, ymax = upper, width=.15)) +
geom_hline(yintercept = 0, linetype = 2) +
theme_light() +
scale_y_continuous(name="Average Marginal Effect", limits = c(-.65, .65)) +
ggtitle("Time Horizon Factors") +
coord_flip()

plot_ME
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
Thiago
  • 173
  • 11
  • 2
    If you convert the independent variable to a factor and specify the order of its levels, it should display that way: https://forcats.tidyverse.org/reference/fct_relevel.html – Jon Spring Dec 19 '18 at 22:09
  • 2
    Possible duplicate of [Change the order of a discrete x scale](https://stackoverflow.com/questions/3253641/change-the-order-of-a-discrete-x-scale) – camille Dec 19 '18 at 22:51

0 Answers0