Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Control order across factors in ggplot2

I have a plot that looks like below. I want to change the order so that the larger value comes first (so cyan would precede red). But I can’t seem to do this. What am I doing wrong?

This is my current code block so far:

ggplot(df, aes(x = Gene.Set.Size, y = OR, label =P.value, color = Method, group = Method)) +
  geom_point(position=position_dodge(width=0.5))  +
  ggrepel::geom_text_repel(size = 6, box.padding = 1, segment.angle = 20, position=position_dodge(width=0.5))+
  geom_pointrange(aes(ymax = UpperCI, ymin = LowerCI),position=position_dodge(width=0.5)) +
  theme_bw() +
  theme(text=element_text(size=25),axis.text.x = element_text(angle = 45, hjust = 1)) +
  ylab("Odds ratio") +
  xlab("Gene set size") +
  theme(plot.margin = unit(c(2,2,2,2), "cm")) 

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> dput(df)
structure(list(Method = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("MAGMA", 
"Pairwise"), class = "factor"), P.value = c(8.74e-28, 1.33e-56, 
5.57e-92, 1.63e-44, 4.23e-71, 2.78e-95), OR = c(1.39, 1.424668, 
1.4, 1.513, 1.478208, 1.409563), UpperCI = c(1.481491, 1.487065, 
1.446039, 1.601557, 1.417117, 1.455425), LowerCI = c(1.316829, 
1.364601, 1.356358, 1.42, 1.541768, 1.365056), Gene.Set.Size = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("500", "1000", "2000"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame")

>Solution :

You must set the factor order.

library(ggplot2)
df <- structure(list(Method = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("MAGMA", 
"Pairwise"), class = "factor"), P.value = c(8.74e-28, 1.33e-56, 
5.57e-92, 1.63e-44, 4.23e-71, 2.78e-95), OR = c(1.39, 1.424668, 
1.4, 1.513, 1.478208, 1.409563), UpperCI = c(1.481491, 1.487065, 
1.446039, 1.601557, 1.417117, 1.455425), LowerCI = c(1.316829, 
1.364601, 1.356358, 1.42, 1.541768, 1.365056), Gene.Set.Size = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("500", "1000", "2000"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame")

#reorder Factor
df$Method = factor(df$Method, levels=c("Pairwise", "MAGMA"))

ggplot(df, aes(x=Gene.Set.Size, y=OR, label=P.value, 
               group= Method, color=Method)) + 
  geom_point(position=position_dodge(width=0.5))  +
  ggrepel::geom_text_repel(size = 6, box.padding = 1, segment.angle = 20, position=position_dodge(width=0.5))+
  geom_pointrange(aes(ymax = UpperCI, ymin = LowerCI),position=position_dodge(width=0.5)) +
  theme_bw() +
  theme(text=element_text(size=25),axis.text.x = element_text(angle = 45, hjust = 1)) +
  ylab("Odds ratio") +
  xlab("Gene set size") +
 theme(plot.margin = unit(c(2,2,2,2), "cm")) 

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading