You could change your data frame to get desired result.
First, make data frame df2
that contains one line for each level of sci
, mat
, apt
and hor
and four lines of rea
. Then make column labels
as factor with levels in order you need (rea
as first, then all other levels). Add column labels2
that contains the same level names as labels
except rea
and each level is used twice (once for the same level and once for rea
).
df2<-rbind(df[-2,],df[rep(2,4),])
df2$labels<-factor(df2$labels,levels=c("rea","sci","mat","apt","hor"))
df2$labels2<-factor(rep(c("sci","mat","apt","hor"),times=2),
levels=c("sci","mat","apt","hor"))
df2
est ucl lcl labels labels2
1 3.0 5.880 1.04 sci sci
3 4.0 7.000 2.04 mat mat
4 1.9 3.724 -0.06 apt apt
5 2.8 5.488 0.84 hor hor
2 2.0 3.920 0.04 rea sci
21 2.0 3.920 0.04 rea mat
22 2.0 3.920 0.04 rea apt
23 2.0 3.920 0.04 rea hor
Now use df2
for the plotting of your data - labels
are used for x axis but labels2
for the facets. Add scales="free_x"
inside facet_wrap()
to remove unused levels in each facet.
ggplot(df2,aes(x = labels, y = est,ymin = lcl, ymax = ucl)) +
geom_point(size = 2.5) +
geom_linerange(size =0.5) +
geom_hline(aes(yintercept = 1)) +
facet_wrap(~labels2,scales="free_x")
UPDATE
To get all lineranges in one plot add to dataframe df2
(made in example above) column type
with two levels - rea
and other
.
df2$type<-rep(c("other","rea"),each=4)
Then use labels2
as x values and type
to set color=
or shape=
, or just group=
for lineranges.
ggplot(df2,aes(x = labels2, y = est,ymin = lcl, ymax = ucl,color=type,shape=type))+
geom_point(position=position_dodge(width=0.5) ,size = 2.5) +
geom_linerange(position=position_dodge(width=0.5), size =0.5) +
geom_hline(aes(yintercept = 1))