add group-level abline to plot

#1

So this graph:


I made with the following code:
logmod <- lm(log(Wt)~log(Palate), data=ex0328Fitch)
summary(logmod)
plot(log(ex0328Fitch$Wt), log(ex0328Fitch$Palate), pch=c(24,21),main="Log Wt vs Palate", xlab="LogPalate",
col=c("red", "blue"), ylab="LogWt"). Before I had it separated by order with different symbols. Now I am trying to figure out basically how to add an abline through each order (the red triangles and blue circles).

Here is what I used for the ggplot:

library(ggplot2)
ggplot(data = ex0328Fitch, aes(x = Palate, y = Wt)) +
geom_point()+geom_smooth(method=lm)

How can I add a 2 order linear regression with parallel slope but different intercept. Also, how would I do it with different slopes?

0 Likes

#2

Hey @bobbers,

Would you please also provide the data set you are using in your script? This will greatly help.

Thank you

0 Likes

#3

I think you need to add a col aesthetic to your aes() in ggplot2. To do this, you will need a column that contains different values for each group. An example with the mpg dataset (contained in the ggplot2 package):

library(ggplot2)

ggplot(data = mpg, aes(x = displ, y = hwy, col = drv)) +
  geom_point() +
  geom_smooth(method=lm)

It looks like you are looking for a linear regression rather than an abline. If you really do want to plot a specific pre-determined intercept and slope, you can use geom_abline():

library(ggplot2)

ggplot(data = mpg, aes(x = displ, y = hwy, col = drv)) +
  geom_point() +
  geom_abline(intercept = 38, slope = -3.8)

If you need multiple lines with a pre-determined slope and intercept, you will need to create a data frame with these parameters to be used for that layer's data (and map the columns to the slope, intercept, and col aesthetics):

library(ggplot2)

line_data <- data.frame(
  drv = c("4", "f", "r"),
  y_int = c(31, 38, 26),
  slope = c(-3, -3.8, -1)
)

ggplot() +
  geom_point(
    mapping = aes(x = displ, y = hwy, col = drv),
    data = mpg
  ) +
  geom_abline(
    mapping = aes(intercept = y_int, slope = slope, col = drv),
    data = line_data
  )

Created on 2019-04-08 by the reprex package (v0.2.1)

1 Like

#5

load("/Users/user/Downloads/ex0328Fitch.rda")
plot(ex0328Fitch)
lmodel<-lm(Wt~Palate, data=ex0328Fitch)
summary(lmodel)
plot(lmodel)
plot(ex0328Fitch$Wt, ex0328Fitch$Palate, main="Wt vs Palate", xlab="Palate", ylab="Wt")
plot(log(ex0328Fitch$Wt), log(ex0328Fitch$Palate), pch=c(24,21),main="Log Wt vs Palate", xlab="LogPalate", col=c("red", "blue"), ylab="LogWt")
legend("topleft", c("Wt","Palate"),pch=c(24,21), pt.bg=c("red", "blue"))
logmod <- lm(log(Wt)~log(Palate), data=ex0328Fitch)
summary(logmod)
plot(log(ex0328Fitch$Wt), log(ex0328Fitch$Palate), pch=c(24,21),main="Log Wt vs Palate", xlab="LogPalate", col=c("red", "blue"), ylab="LogWt")
fit <- lm(log(Wt)~log(Palate), data=ex0328Fitch)
summary(fit)

Order Wt Skull Palate

1 Coyote Carnivora 14.500 19.70 9.47
2 Grizzly bear Carnivora 306.000 41.10 20.30
3 Wolverine Carnivora 15.000 16.70 8.24
4 Lion Carnivora 175.000 37.40 18.10
5 Leopard Carnivora 50.300 22.60 9.41
6 Gray Wolf Carnivora 33.000 27.80 13.60
7 Kit fox Carnivora 1.820 11.39 5.78
8 Fennec Fox Carnivora 1.250 8.86 4.25
9 Tiger Carnivora 113.000 32.70 15.20
10 Jaguar Carnivora 89.000 26.20 10.98
11 Cougar Carnivora 70.900 21.30 8.40
12 Maned wolf Carnivora 23.800 22.70 10.78
13 Fisher Carnivora 6.800 13.06 6.29
14 Leopard seal Carnivora 270.000 28.60 12.60
15 Gorilla Primates 170.000 30.80 11.82
16 Baboon Primates 32.500 16.70 7.31
17 Mouse lemur Primates 0.070 3.19 1.33
18 Bush-baby Primates 0.210 4.45 1.59
19 Capuhcin Primates 2.200 9.80 3.55
20 Black howler Primates 6.700 12.88 4.89
21 Macaque Primates 3.820 10.90 4.53
22 Proboscis monkey Primates 14.000 12.16 4.54
23 Orangutan Primates 54.400 23.95 10.30
24 Dwarf lemur Primates 0.389 5.68 2.44
25 Avahi Primates 0.900 5.44 1.50
26 Sportive lemur Primates 0.700 4.94 1.73
27 Potto Primates 1.200 6.36 2.25
28 Monk saki Primates 1.200 8.95 3.30
0 Likes

#6

Please take a moment to familiarise yourself with our site's homework policy (see below):

0 Likes

#7

Also, please post your code and data on a copy / paste friendly format, and preferably using the reprex package, if you do not know how, here is a FAQ that explains how to do it.

For example, this would be a reprex for your question

df <- data.frame(
    stringsAsFactors = FALSE,
    Wt = c(
        14.5, 306, 15, 175, 50.3, 33, 1.82, 1.25, 113, 89, 70.9, 23.8,
        6.8, 270, 170, 32.5, 0.07, 0.21, 2.2, 6.7, 3.82, 14, 54.4,
        0.389, 0.9, 0.7, 1.2, 1.2
    ),
    Skull = c(
        19.7, 41.1, 16.7, 37.4, 22.6, 27.8, 11.39, 8.86, 32.7, 26.2,
        21.3, 22.7, 13.06, 28.6, 30.8, 16.7, 3.19, 4.45, 9.8, 12.88,
        10.9, 12.16, 23.95, 5.68, 5.44, 4.94, 6.36, 8.95
    ),
    Palate = c(
        9.47, 20.3, 8.24, 18.1, 9.41, 13.6, 5.78, 4.25, 15.2, 10.98,
        8.4, 10.78, 6.29, 12.6, 11.82, 7.31, 1.33, 1.59, 3.55, 4.89,
        4.53, 4.54, 10.3, 2.44, 1.5, 1.73, 2.25, 3.3
    ),
    Species = c(
        "Coyote", "Grizzly bear", "Wolverine", "Lion",
        "Leopard", "Gray Wolf", "Kit fox", "Fennec Fox",
        "Tiger", "Jaguar", "Cougar", "Maned wolf", "Fisher",
        "Leopard seal", "Gorilla", "Baboon", "Mouse lemur",
        "Bush-baby", "Capuhcin", "Black howler", "Macaque",
        "Proboscis monkey", "Orangutan", "Dwarf lemur", "Avahi",
        "Sportive lemur", "Potto", "Monk saki"
    ),
    Order = c(
        "Carnivora", "Carnivora", "Carnivora", "Carnivora",
        "Carnivora", "Carnivora", "Carnivora", "Carnivora",
        "Carnivora", "Carnivora", "Carnivora", "Carnivora",
        "Carnivora", "Carnivora", "Primates", "Primates",
        "Primates", "Primates", "Primates", "Primates", "Primates",
        "Primates", "Primates", "Primates", "Primates",
        "Primates", "Primates", "Primates"
    )
)

library(ggplot2)

ggplot(df, aes(x = log(Wt), y = log(Palate), shape = Order, color = Order)) +
    geom_point() +
    geom_smooth(method = "lm")

1 Like

#8

Thank you andrercs. Hey how did you get those data in the dataframes, did you manually type them in, or is there an easy r-code for that? (to extract the data from a data file I have loaded in my R console. )

0 Likes

#9

I have used datapasta, read the link I gave you before about reprex and you will learn how to do the same

1 Like

#10

Ok thanks. Yeah I looked at that and it is starting to make sense. I can't get that pasta package to load on my R though, :frowning:

But my next question is, how would I fit the general model that produces parallel regression lines for the two levels of Noise? (IS this saying they are just different intercepts but same slope? How to tell r this?)

0 Likes

closed #11

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes