Reorder variables in ggplot

66 Views Asked by At

I'm trying to make a forestplot with ggplot but I can't find a way to reorder the variables. In this case I am making a foresplot of moderators and therefore I would like the values of the same time to appear together. In this case, for example, I would like the data for the 12-weeks of the old ones to appear and then the 12 weeks of the young ones from the first outocome, then the 24 weeks of both and then the following variable. Any suggestions?

This is the code that I used:

tiff("plotA.1.tiff", units = "in", width = 25, height = 15, res = 300)
ggplot(data_long, aes(x = change, y = outcome)) +
 
  geom_point(aes(y = outcome, shape = type_high_low, color = factor(time, levels = rev(levels(factor(time))))),
             position = position_dodge(width = 0.8), size = 5) +
  geom_errorbar(aes(y = outcome ,shape = type_high_low,color = factor(time, levels = rev(levels(factor(time)))), x = change,  
                  xmin = Change_lowerci, xmax = Change_upperci), width = 0, position = position_dodge(width = 0.8)) +
  
  geom_text(aes(shape = type_high_low, label = ifelse(p_value > 0.05,format(round(change, digits=3)), paste0(format(round(change, digits=3)),"*")),
    color = factor(time, levels = rev(levels(factor(time))))), hjust = -0.6, vjust = -0.5, size = 3, position = position_dodge(width = 0.8))+
  
    geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  theme_bw() +
  facet_wrap(~ mode, ncol = 9, scales="free_x") +
  labs(x = "\u2190 Favors control   Favors Exercise \u2192",
       y="Executive function variables",
       title = "Moderators",
       caption = "Figure 3. ") + 
  scale_color_manual(name = "Time", 
                     values = colors) + 
  scale_shape_manual(name = "type_high_low",  
                     values = c("Lower" = 16, "Higher" = 17),
                     labels = c("Lower" ,
                                "Higher")) +
  guides(color = guide_legend(reverse = TRUE), 
         shape = guide_legend()) +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        axis.title.x = element_text(size = 20), 
        strip.text = element_text(size = 20, face = "bold"), 
        strip.text.x = element_text(size = 20, face = "bold"),  
        legend.text = element_text(size = 10))  
        
dev.off()

In the photo you can see the shape as time and the colours as the group. Now is 12- 24 week of the older and younger and I want to see 12w older 12w younger 24w older 24 younger.

Thansk so much enter image description here

In the photo you can see the shape as time and the colours as the group. Now is 12- 24 week of the older and younger and I want to see 12w older 12w younger 24w older 24 younger.

0

There are 0 best solutions below