1
votes

I am creating a plot that shows the confidence intervals of two models for each factor. So if my factors are 'A', 'B', 'C', I have six confidence intervals CI1.A, CI2.A, CI1.B, CI2.B, CI1.C, CI2.C. I am using a simple forest plot but want the y-axis labels to be A, B, C, where A is in the middle of CI1.A and CI2.A. How can I rearrange the tick marks to make them appear in the middle of two factors?

Here is a toy example. I have 49 factors so I need a way to be able to read all of the labels.

factors <- c('A1', 'A2', 'B1', 'B2', 'C1', 'C2')
y <- c(1:6)
yhi <- y + .5
ylo <- y - .5

df <- data.frame(factors = factors, y = y, yhi =yhi, 
             ylo = ylo)

ggplot(df, aes(x=factors, y=y, ymin=ylo, ymax=yhi)) + 
geom_linerange() +
coord_flip()  
1
For my answer I assume that you meant "want the y-axis labels to be A, B, C, where A is in the middle of CI1.A and CI2.***A***". If that wasn't a typo, I have no idea what you want so my answer may not be helpful. - Curt F.
yes! edited, thank you! - sam_eman

1 Answers

0
votes

I think your best bet is to change your data frame so that the factor and the model are separate columns.

That way, the ticks automatically come out the way you want them. By default, geom_linerange() would position lineranges for the two different models on top of each other, but you can change that with position=position_dodge(width=<<NUMBER>>). I used 0.1 to put the lines almost but not quite on top of each other. Increase the value if you want them farther apart. What I think you originally had in mind would be about width=1.

#load package
require(ggplot2)

#make toy data
models <- rep(c(1,2), 3)
factors <- c('A', 'A', 'B', 'B', 'C', 'C')
df <- data.frame(factors=factors, models=factor(models), 
                 y=1:6, yhi=1:6+0.5, ylo=1:6-0.5)

#do graphing
quartz(height=3, width=6)
ggplot(df, aes(x=factors, y=y, ymin=ylo, ymax=yhi, color=models)) + 
   geom_linerange(size=2, position=position_dodge(width=0.1)) +
   coord_flip() +
   theme_bw()
quartz.save('SO_29545579.png')

enter image description here