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

Plot factors side by side in ggplot2

I have a table as shown below, and I would like to make a scatterplot whereby the points for each factor is side by side instead. Is that possible?

fishers.drugs %>%
  ggplot(aes(fill = Method, color=Method, x=Phenotype, y=OR, ymin = Lower.CI, ymax = Upper.CI)) +
  geom_errorbar(aes(color=Method), show.legend = FALSE)+
  geom_point(shape=21, size=12) +
  scale_fill_manual(values = c("yellow", "cyan4"))+
  scale_color_manual(values = c("black","black"))+
  theme_bw(base_size = 20) +
  theme(text=element_text(size=34),axis.text.x = element_text(hjust = 1))  

enter image description here

Data:

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

fishers.drugs <- structure(list(Phenotype = c("SCZ", "BD", "MDD", "PTSD", "SCZ", 
"BD", "MDD", "PTSD"), P.value = c(3.6e-05, 7.6e-06, 0.0023, 0.03, 
0.29, 0.043, 0.19, 0.71), Method = c("Pairwise", "Pairwise", 
"Pairwise", "Pairwise", "MAGMA", "MAGMA", "MAGMA", "MAGMA"), 
    OR = c(3.1, 3.7, 2.53996, 2.6, 1.27, 1.9, 1.574722, 0.8), 
    Lower.CI = c(1.80232, 2.124302, 1.352569, 0.9064928, 0.5729662, 
    0.9067536, 0.7052697, 0.09566171), Upper.CI = c(4.846504, 
    6.090133, 4.40825, 5.8462159, 2.4825332, 3.6740963, 3.0758353, 
    3.01399383)), class = "data.frame", row.names = c(NA, -8L
))

>Solution :

Is this what you are picturing? I’ve changed positions for both points and error bars.


library(ggplot2)
library(dplyr)


fishers.drugs %>%
  ggplot(aes(fill = Method, color=Method, x=Phenotype, y=OR, ymin = Lower.CI, ymax = Upper.CI)) +
  geom_errorbar(aes(color=Method), show.legend = FALSE, position = position_dodge(width = 1))+
  geom_point(shape=21, size=12, position = position_dodge(width = 1)) +
  scale_fill_manual(values = c("yellow", "cyan4"))+
  scale_color_manual(values = c("black","black"))+
  theme_bw(base_size = 20) +
  theme(text=element_text(size=34),axis.text.x = element_text(hjust = 1)) 

Created on 2023-04-06 with reprex v2.0.2

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