2

I hope this comes as an easy question for some of you. I am basically plotting the Average Marginal Effects of several variables based on a logistic regression:

summary(AMElogmodel1 <- glm(Smartphone ~ sf_group + sf_sex + age + sf_marital + sf_big + sf_hhsize, 
                        data = Selectsfinal, family = binomial(link = "logit"), na.action = na.exclude))

And then, in order to have the plot with ggplot instead of the default AME plot for this cases I did this (mainly because I wanted to revert the X and Y axis):

xxx <- summary(margins(AMElogmodel1))
ggplot(data = xxx) +
  geom_point(aes(factor, AME)) +
  geom_errorbar(aes(x = factor, ymin = lower, ymax = upper)) +
  geom_hline(yintercept = 0) +
  coord_flip() +
  theme_minimal() +
  labs(x='Variable', y ="Average marginal effect")

Leading to this graph: enter image description here

As you can see, the names displayed there are the technical names for the variables and their categories. I imagine that if I change their names and labels this would work, but then it would mess with all my other code. Is there a way to change the names? So instead of sf_sexMale, Male and instead of age66-76, Age 66-76? Also, is there a way to change the order in which they are displayed?

Thank you very much for your help.

Bloxx
  • 1,495
  • 1
  • 9
  • 21
  • 2
    This should work, try adding +scale_x_discrete(labels=c("sf_sexMale"="Male", "age66-76"="Age 66-76")) Although I'm sure there is a more efficient way. – cgvoller Oct 28 '21 at 08:54
  • Thanks, it was that! Any tips on how to change the order in which they are displayed? – Marc Asensio Manjon Oct 28 '21 at 09:42
  • 1
    This might offer the solution, it reorders the axis without changing the data https://stackoverflow.com/questions/12774210/how-do-you-specifically-order-ggplot2-x-axis-instead-of-alphabetical-order – cgvoller Oct 28 '21 at 10:36
  • @cgvoller Want to paste your solution into an answer, so this question is not flagged as open? – Roman Nov 15 '21 at 03:17

0 Answers0