I am generating a plot to show the estimates and CI graphically. Right now my estimates and y axis labels are out of synch on the plot.
If this is my dataset.
Group Est conf.low conf.high pvalue
Bi 1.12 0.65 1.603 0.000
Di -0.24 -0.44 -0.038 0.02
Dn -0.47 -0.80 -0.140 0.005
HMD -0.006 -0.32 0.311 0.968
HMS 0.72 -1.00 -0.436 0.000
LM -0.055 -0.32 0.214 0.6886
PaS -1.31 -1.79 -0.850 0.000
Ph A 0.065 -0.250 0.381 0.6885
TRB 1.023 0.63 1.41 0.000
TRC -0.599 -0.94 -0.249 0.0008
What I am seeing is a plot like this below where the y axis labels and estimates are completely out of order.
This is my code
ggplot(df, aes(y = row.names(df), x= Est))
geom_point(shape = 16, size =5)
scale_y_discrete(label = df$Group)
geom_errobarh(aes(xmin = conf.low, xmax = conf.high), width =0, size = 1)
geom_vline(xintercept=1, color="black", linetype = "dashed", cex=1, alpha=0.5)
Any help on how to sync my y-axis labels with the estimates is much appreciated. Thanks.
CodePudding user response:
If you want the plot to appear in the same order as the data in your data frame, then you need to plot Group
on the y axis, but first convert it to a factor whose levels are the reverse of the column Group
:
library(ggplot2)
ggplot(df, aes(y = factor(Group, rev(Group)), x = Est))
geom_point(shape = 16, size = 5)
geom_text(aes(label = Est), nudge_y = 0.3, size = 4)
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0, size = 1)
geom_vline(xintercept = 1, linetype = "dashed", cex = 1, alpha = 0.5)
labs(y = "Group")
theme_gray(base_size = 16)
Reproducible data
df <- structure(list(Group = c("Bi", "Di", "Dn", "HMD", "HMS", "LM",
"PaS", "Ph A", "TRB", "TRC"), Est = c(1.12, -0.24, -0.47, -0.006,
-0.72, -0.055, -1.31, 0.065, 1.023, -0.599), conf.low = c(0.65,
-0.44, -0.8, -0.32, -1, -0.32, -1.79, -0.25, 0.63, -0.94), conf.high = c(1.603,
-0.038, -0.14, 0.311, -0.436, 0.214, -0.85, 0.381, 1.41, -0.249
), pvalue = c(0, 0.02, 0.005, 0.968, 0, 0.6886, 0, 0.6885, 0,
8e-04)), class = "data.frame", row.names = c(NA, -10L))
CodePudding user response:
Credits: I had forgotten the values above the points, correction copy&pasted from Allan Cameron's answer.
Here is a solution with reorder
to put the y axis in order of the bars mid points, Est
.
df1<-"Group Est conf.low conf.high pvalue
Bi 1.12 0.65 1.603 0.000
Di -0.24 -0.44 -0.038 0.02
Dn -0.47 -0.80 -0.140 0.005
HMD -0.006 -0.32 0.311 0.968
HMS -0.72 -1.00 -0.436 0.000
LM -0.055 -0.32 0.214 0.6886
PaS -1.31 -1.79 -0.850 0.000
'Ph A' 0.065 -0.250 0.381 0.6885
TRB 1.023 0.63 1.41 0.000
TRC -0.599 -0.94 -0.249 0.0008"
df1 <- read.table(textConnection(df1), header = TRUE)
library(ggplot2)
ggplot(df1, aes(x = Est, y = reorder(Group, -Est)))
geom_pointrange(aes(xmin = conf.low, xmax = conf.high), size = 1)
geom_text(aes(label = Est), nudge_y = 0.3, size = 4)
geom_vline(xintercept = 1, linetype = "dashed", alpha = 0.5)
ylab("Group")
Created on 2022-10-29 with reprex v2.0.2
And with error bars, instead of line bars. This time the points need an explicit call to geom_point
.
ggplot(df1, aes(x = Est, y = reorder(Group, -Est)))
geom_point(pch = 16, size = 5)
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0.5, size = 1)
geom_text(aes(label = Est), nudge_y = 0.3, size = 4)
geom_vline(xintercept = 1, linetype = "dashed", alpha = 0.5)
ylab("Group")
Created on 2022-10-29 with reprex v2.0.2