0

I have the following dataframe:


mlt = structure(list(V1 = c("Female > 55", "Male > 55", "Finance Industry", 
"South"), climatechange = c(0.116, 0.093, -0.186, 0.311), `SE climatechange` = c(0.299720594648884, 
0.209685397712576, 0.198822469025311, 0.220794746780239), housing = c(-0.223, 
0.288, -0.063, -0.401), `SE housing` = c(0.240842826106496, 0.262613125585529, 
0.233796203318591, 0.112279926339202), macro = c(-0.219, -0.243, 
0.252, 0.058), `SE macro` = c(0.167027449787956, 0.109527362813708, 
0.225740740488032, 0.139837005156927), `pension and savings` = c(0.287, 
0.815, 0.348, -0.611), `SE pension and savings` = c(0.278129111903788, 
0.26508680711124, 0.250889015745089, 0.0571662145564419)), row.names = c(NA, 
-4L), class = "data.frame")

View(df)

The dataframe has 4 topics and for each topic the corresponding standard error. So far, I plotted just the first topic:

mlt1 = mlt[,c(1:3)]

ggplot(mlt1, aes(V1, climatechange,
                 ymin = climatechange - 1.96*`SE climatechange`, ymax= climatechange + 1.96*`SE climatechange`)) +
  scale_x_discrete('') +
  scale_y_continuous('Marginal Effect \n',limits=c(-1,1)) +
  theme_classic() +
  theme(panel.border = element_rect(fill=NA)) + geom_errorbar(aes(x = V1, y = climatechange),
                                                              size=0.8,width=.2, col = "#0072B2") + 
  geom_point(aes(fill=factor(climatechange)), size=5, shape = 21) +
  scale_fill_manual(values=c(rep("#0072B2",13))) + 
  geom_hline(yintercept=0, col = "grey")  + theme(legend.position="none") + theme(axis.text = element_text(color = "black", size = 12),
                                                       axis.title = element_text(size = 12)) + labs(title="Climate Change") +
  theme(plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5))

What I would like to do, though, is to get a graph that looks like this:

enter image description here

where instead of A,B,... I have my four topics and for each topic the corresponding variables displayed. Looking around, I saw that someone used: facet_grid(.~Test, space = "free_x", shrink = T, scales = "free_x"). Yet; I am not sure how to apply to my dataset.

Can anyone help me?

Thanks!

1 Answer 1

2

To do for all four - first you need to pivot a couple of times to make the dataframe tidy (i.e. create a column for measure (topic), mean and SE), then create the dataframe faceting by the measure variable:

mlt %>% 
  pivot_longer(2:9, 
               names_to = c("measure"), 
               values_to = "est") %>%
  mutate(stat = ifelse(str_detect(measure, "^SE"), "SE", "mean"),
         measure = str_extract(measure, "\\w*$")) %>% 
  pivot_wider(names_from = "stat",
              values_from = "est", 
              id_col = c(V1, measure)) %>% 
  ggplot(aes(x = V1, mean,
             ymin = mean - 1.96*SE,
             ymax = mean + 1.96*SE)) +
  scale_x_discrete('') +
  scale_y_continuous('Marginal Effect \n', 
                     # limits=c(-1,1)
  ) +
  coord_cartesian(ylim = c(-1, 1)) +
  theme_classic() +
  theme(panel.border = element_rect(fill=NA)) + 
  geom_errorbar(size=0.8,width=.2, col = "#0072B2") + 
  geom_point(size=5, shape = 21) +
  scale_fill_manual(values=c(rep("#0072B2",13))) + 
  geom_hline(yintercept=0, col = "grey")  + 
  theme(legend.position="none") + 
  theme(axis.text = element_text(color = "black", size = 12),
        axis.title = element_text(size = 12)) + labs(title="Climate Change") +
  theme(plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5)) +
  facet_wrap(~measure, nrow = 1)

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Almost. I guess you just need to flipp it around. The columns should be: climate change, housing, macro and pension and savings. Then, in each of this group, I shall have points and bands for female >55, male>55, Finance industry, south. I'd be grateful forever if you manage to get that
Ah I see! so a little transformation of your dataframe should make it straightforward. will update in a second...
hope that's helpful - do let me know if more notes explaining how things are working would be helpful, but if it's clear enough then good luck with ongoing steps!
sorry had just taken out fill command to make it tidier. take out shape = 21 to get it to default back to a filled dot, or set fill = "blue" or similar just after it.
should have also checked - do you want the fill to change by topic? then fill = measure inside it would colour differently for each facet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.