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:
summary()
re-orders the independent variables alphabetically [I don’t know why], and;- 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