user41509
user41509

Reputation: 1040

plot different values for same x axis value without overlapping using R

I am trying to plot parameter estimates from 2 different models on the same plot with confidence intervals. The values for each parameter are overlapping and I would like to have the values for each x value (parameter) side by side.

Data

ci_glm<-   dput(head(ci_glm))
structure(list(parameter = c(-0.989960390831752, 0.23802371512626, 
-0.0616305811832892, -1.19145279737722, -0.39565631764158, -2.70713419498971
), lower = c(-1.76343111098339, -0.0384145902617419, -0.338256948651047, 
-2.41452042708909, -1.56076899496423, -3.8540190563328), upper = c(0.037528250100757, 
0.514419550437131, 0.214814325315589, -0.0336965457336884, 0.639688082685478, 
-1.70838885452134), par = c("Intercept", "Vessel 2", "Vessel 3", 
"10", "11", "13")), .Names = c("parameter", "lower", "upper", 
"par"), row.names = c("(Intercept)", "as.factor(CruiseID)201502", 
"as.factor(CruiseID)201503", "as.factor(Stratum)10", "as.factor(Stratum)11", 
"as.factor(Stratum)13"), class = "data.frame")

ci_boot<-dput(head(ci_boot2))
structure(list(parameter = c(-1.23409264614473, NA, NA, -0.434928403121171, 
-2.74151010196932, -0.361626461606862), lower = c(-1.99928925205138, 
NA, NA, -2.16613527555384, -1.38979210854727, -3.9529283095427
), upper = c(-0.118870916073164, NA, NA, 0.0238247660480798, 
0.860847808652077, -1.44129237641604), par = c("Intercept", "Vessel 2", 
"Vessel 3", "10", "11", "13"), diff = c(1.88041833597822, NA, 
NA, 2.18996004160192, 2.25063991719935, 2.51163593312666), diff1 = c(0.76519660590665, 
NA, NA, 1.73120687243267, -1.35171799342205, 3.59130184793584
), diff2 = c(1.11522173007157, NA, NA, 0.458753169169251, 3.6023579106214, 
-1.07966591480918)), .Names = c("parameter", "lower", "upper", 
"par", "diff", "diff1", "diff2"), row.names = c("(Intercept)", 
"1", "11", "as.factor(Stratum)10", "as.factor(Stratum)11", "as.factor(Stratum)13"
), class = "data.frame")

plot

require(plotrix)

plot(ci_glm$parameter, type='n', ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4)) 
axis(1, at=1:6, labels=unique(ci_glm$par),las=3,cex.axis=0.8) 
axis(2)
abline(h=0,lty=2,col="light gray") 

plotCI(ci_glm$parameter,y=NULL,uiw = abs(ci_glm$upper-ci_glm$parameter),liw=abs(ci_glm$parameter-ci_glm$lower),
    ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
    add=T,scol="black",pch=18,pt.bg=par("bg",col="black"))

par(new=T)

plot(ci_boot$parameter, type='n',ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4),col="red") 
axis(2)
abline(h=0,lty=2,col="light gray") 

plotCI(ci_boot$parameter,y=NULL,uiw = abs(ci_boot$upper-ci_boot$parameter),liw=abs(ci_boot$parameter-ci_boot$lower),
    ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
    add=T,scol="red",pch=18,pt.bg=par("bg",col="red"))

leg.text<-c("GLM","GLMM")
legend("bottomright",leg.text,lty=c(1,1),pch=c(20,20),text.col="black",col=c("black","red"),bty="y",cex=.8)

R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Upvotes: 0

Views: 229

Answers (1)

Mark Peterson
Mark Peterson

Reputation: 9570

If you are willing to switch to using ggplot2, this can be done easily with position_dodge(), as seen here. I am also using dplyr to combine the two model outputs into one plottable data.frame.

toPlot <-
  bind_rows(boot = ci_boot %>% mutate(Parameter = row.names(.))
            , glm = ci_glm %>% mutate(Parameter = row.names(.))
            , .id = "Model")


ggplot(
  toPlot
  , aes(x = Parameter
        , y = parameter
        , ymin = lower
        , ymax = upper
        , col = Model)) +
  geom_linerange(position = position_dodge(0.2)) +
  geom_point(position = position_dodge(0.2)) +
  theme(axis.text.x = element_text(angle = 45
                                   , hjust = 1))

Note that, for a couple of your bootstrap model parameters, the estimate is somehow outside the range of your confidence interval. You will likely want to clean up the labels etc. (and either remove or supply values for 1 and 11), but this addresses the overlap question.

enter image description here

Upvotes: 1

Related Questions