Search code examples
rggplot2lineaddition

add many straight line with given slopes and intercepts in R (output of lmer package)


I have a mixed-effect model "logit_ri" (random intercept) from glmer function. I try to extract the fixed effect slope and the random intercept into a dataframe, and then plot it by group "Land" (German states)

# Extract out the fixed-effect slope 
slope <- fixef(logit_ri)['logASLr:Artl']

# Extract out the random-effect intercept for Land
Land_intercept <- ranef(logit_ri)$Land

# Create a new column for the slope
Land_intercept$slope <- slope

# Use the row names to create a county name column
Land_intercept$Land <- rownames(Land_intercept)

x <- c(seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2))
y <- c(seq(1:16))
Land_intercept <- data.frame(cbind(Land_intercept,x,y))
colnames(Land_intercept)[1]<- "intercept"


the dataframe looks like this:

Land_intercept
                          intercept     slope                   Land x  y
Baden-W¸rttemberg      -0.161346161 -2.366151      Baden-W¸rttemberg 1  1
Bayern                  0.008278875 -2.366151                 Bayern 2  2
Berlin                 -0.105067965 -2.366151                 Berlin 1  3
Brandenburg             0.507153607 -2.366151            Brandenburg 2  4
Bremen                 -0.217788246 -2.366151                 Bremen 1  5
Hamburg                -0.660789497 -2.366151                Hamburg 2  6
Hessen                 -0.217997505 -2.366151                 Hessen 1  7
Mecklenburg-Vorpommern  0.262814205 -2.366151 Mecklenburg-Vorpommern 2  8
Niedersachsen          -0.182076161 -2.366151          Niedersachsen 1  9
Nordrhein-Westfalen     0.015583050 -2.366151    Nordrhein-Westfalen 2 10
Rheinland-Pfalz        -0.239750889 -2.366151        Rheinland-Pfalz 1 11
Saarland               -0.164090824 -2.366151               Saarland 2 12
Sachsen                 0.373352938 -2.366151                Sachsen 1 13
Sachsen-Anhalt          0.443658186 -2.366151         Sachsen-Anhalt 2 14
Schleswig-Holstein     -0.098413476 -2.366151     Schleswig-Holstein 1 15
Th¸ringen               0.436479863 -2.366151              Th¸ringen 2 16

and plot it by ggplot2 (by "Land")

ggplot(data=Land_intercept,
   aes(x,y))+
  geom_point()+
  theme_bw() +
  scale_y_continuous(limits=c(-3,1))+
  geom_abline(data=Land_intercept,aes(slope=slope,intercept=intercept,color=factor(Land)))

However, the intercept in my plot is not correct. I would appreciate any helps on this. enter image description here Since I can't upload the raw data (it's huge),I may need helps to plot from the data including 16 slopes (which are all the same) and 16 intercepts for 16 German states . (as I posted above)

Better with labels also on these lines to make it more clear to read.

Or some elegant ways to plot random intercept model with slope and intercepts together would also be great.

Thank you in advance for any advices and helps.


Solution

  • RE: comments

    You don't need x and y aesthetics for the plot to work. For the grid to show up, you must have axis limits though. This makes sense if you think about lines parametrised by y = a + bx stretching to infinity.

    library(ggplot2)
    
    Land_intercept <- data.frame(
      intercept = c(-0.161346161,0.008278875,
                    -0.105067965,0.507153607,-0.217788246,-0.660789497,
                    -0.217997505,0.262814205,-0.182076161,0.01558305,
                    -0.239750889,-0.164090824,0.373352938,0.443658186,-0.098413476,
                    0.436479863),
      slope = c(-2.366151,-2.366151,-2.366151,
                -2.366151,-2.366151,-2.366151,-2.366151,-2.366151,
                -2.366151,-2.366151,-2.366151,-2.366151,-2.366151,
                -2.366151,-2.366151,-2.366151),
      Land = c("Baden-W¸rttemberg","Bayern",
               "Berlin","Brandenburg","Bremen","Hamburg","Hessen",
               "Mecklenburg-Vorpommern","Niedersachsen",
               "Nordrhein-Westfalen","Rheinland-Pfalz","Saarland","Sachsen",
               "Sachsen-Anhalt","Schleswig-Holstein","Th¸ringen")
    )
    
    ggplot(Land_intercept) +
      geom_abline(aes(slope = slope, intercept = intercept, colour = Land)) +
      xlim(c(-1, 1)) + ylim(c(-3,  1))
    

    Created on 2021-08-19 by the reprex package (v1.0.0)