tabtimm
tabtimm

Reputation: 431

How to add fixed-slope line in multipanel plot using different intercepts in each panel

I generate a multipanel plot using ggplot2 (requires ggplot2 package). Each of the ten panels represents a different plant growth form. I need to plot a line in each panel that has a slope of 0.704. However, the intercept should be different for each panel, for example -7.9 in the case of 'fern', -7.31 for 'fern ally', etc. Currently, I use the code below which generates a line where both slope and intercept are the same in each panel:

ggplot(veg, aes(x=ord1, y=log(lai+0.000019))) +    
scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
scale_y_continuous(limits=c(-12,3)) +
geom_point(shape=1) +
geom_segment(aes(x = 1, xend = 9, y = -8.32 + .704, yend = -8.32 + .704*9), 
           col = "black", size = 1, lty="longdash", lwd=1) +
facet_wrap( ~ plant_growth_form, ncol=5) 

How could I modify this code in ggplot2 to specify a different intercept for each growth form?

A reproducible data subset generated with dput() can be found at: How to compute standard errors for predicted data

Upvotes: 0

Views: 1086

Answers (1)

KFB
KFB

Reputation: 3501

First, create a separate data frame housing intercepts and slope. e.g. "a" is intercepts, "b" is the slope. We call this data frame "df3".

df3 <- data.frame(plant_growth_form = unique(veg[,3]),
                  a = c(-10,-9,-8,-7,-6,-5,-4,-3,-2,-1), # assigned arbitrarily
                  b= 0.704)

#     plant_growth_form   a     b
# 1                herb -10 0.704
# 2                moss  -9 0.704
# 3       woody climber  -8 0.704
# 4        tree sapling  -7 0.704
# 5                fern  -6 0.704
# 6  herbaceous climber  -5 0.704
# 7               undet  -4 0.704
# 8    herbaceous shrub  -3 0.704
# 9               grass  -2 0.704
# 10        woody shrub  -1 0.704

to plot

ggplot(veg, aes(x=ord1, y=log(lai+0.000019))) +    
  scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
  scale_y_continuous(limits=c(-12,3)) +
  geom_point(shape=1) +
  geom_abline(aes(intercept=a, slope=b), data=df3) +
  facet_wrap( ~ plant_growth_form, ncol=5)

enter image description here

update the tweak per @aosmith's comment.

# you could separately modify the dataframe of intercepts & slope, or
# adjust directly in the code below for small data (see <--)
df3 <- data.frame(plant_growth_form = unique(veg[,3]),
                  a = c(-10,-9,-8,-7,-6,-5,-4,-3,-2,-1),  # <-- here
                  b= 0.704)                               # <-- here

# to add the code of extra segment (see <--), in red.
ggplot(veg, aes(x=ord1, y=log(lai+0.000019))) +    
  scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
  scale_y_continuous(limits=c(-12,3)) +
  geom_point(shape=1) +
  #geom_abline(aes(intercept=a, slope=b), data=df3) +
  geom_segment(data = df3, aes(x = 1, xend = 9, y = a + b, yend = a + b*9)) +
  geom_segment(aes(x = 1, xend = 9, y = -8.32 + .704, yend = -8.32 + .704*9), # <-- here
               col = "red", size = 1, lty="longdash", lwd=1) +
  facet_wrap( ~ plant_growth_form, ncol=5) 

enter image description here

Upvotes: 1

Related Questions