fixed the issue. Will look into that further Several suggestions for consideration...
1: Wrangle your data frame before feeding it into ggplot(). Since trait is a categorical variable, you can specify its levels as a factor explicitly.
library(dplyr)
g2 <- GEE %>%
mutate(trait = factor(trait, levels = trait[order(beta[analysis == "group1"])]))
> levels(g2$trait)
[1] "trait3" "trait5" "trait4" "trait2" "trait1"
ggplot(GEE, aes(x = beta, y = analysis, label = trait)) + geom_label()
ggplot(g2, aes(y = beta, x = trait, group = analysis, color = analysis,
ymin = beta - 2*se, ymax = beta + 2*se)) +
geom_point(aes(alpha = signif), # hide non-significant points while using the same dataset
color = "red") + # override 'color = analysis' in ggplot()
geom_point() +
geom_errorbar() +
scale_alpha_identity()
pd <- position_dodge(0.9)
ggplot(g2, aes(y = beta, x = trait, group = analysis, color = analysis,
ymin = beta - 2*se, ymax = beta + 2*se)) +
geom_point(aes(alpha = signif),
color = "red", shape = "*", size = 12, show.legend = F,
position = pd) +
geom_point(position = pd) +
geom_errorbar(width=.2, position = pd) +
scale_alpha_identity() +
theme_light() +
coord_flip()
GEE <- read.csv(
text =
"trait,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0")
> levels(GEE$trait) # default order is 1-2-3-4-5
[1] "trait1" "trait2" "trait3" "trait4" "trait5"