I have a problem to code my ggplot. I have this kind of dataset, about organ size related to body length (in x):
Species <- c("dog","dog","dog","dog","cat","cat","cat","cat")
CatAge <- c("young","young", "old","old", "young","young", "old","old" )
Organ <- c("Head", "Tail", "Head", "Tail","Head", "Tail","Head", "Tail")
a0 <- c(2.98962815, 0.53920924, 1.51232011, 0.32431237, 1.63165924, 0.1801883, 2.18294768, 0.10224673)
a1 <- c(-0.123974545, 0.013143765, 1.261783376, 0.290707994, 1.366204115, -0.009038363, 0.60089694, 0.265277471)
xmin <- c(0.9334873, 0.9334873, 1.1205739, 1.1205739, 0.5250448, 0.5250448, 0.6599162, 0.6599162)
xmax <- c(1.0629578, 1.0629578,1.2193225,1.2193225, 0.6946052, 0.6946052, 0.8331471, 0.8331471 )
MyData <- data.frame(Species, CatAge, Organ, a0, a1, xmin, xmax)
With Species the considered species, CatAge the category of age, Organ the measured organ, a0 and a1 the coefficients in the formula a0+a1*x, and xmin and xmax the body length who are the limits of the linear regression
(Note: this is a totally unrealistic dataset, but my dataset has a similar structure: Species, Larval instar, measured organs and the coefficients and limits)
What I want is this type of graph (with red/brown head/tail of old animal, blue/green Head/tail of young one):
Basically, I just want to tell to ggplot: draw the linear regression using a0 and a1, within the limits of xmin and xmax, in the correct panel, and apply a color according to the combinations Organ/CatAge.
So I though a code like that
ggplot(data=MyData, aes(group=Species))+
facet_wrap(~Species)+
stat_function(data= MyData, fun=function(x){a0+a1*x}, xlim=c(xmin, xmax) )
Of course, didn't work. In my attempts, I had many error messages: attempt to apply non-function, xmin and xmax unknown, Aesthetics must be either length 1 or the same as the data (18): x etc etc
I even considered to pass by a loop "for" to consider the dataset line by line, like
g+ stat_function(data = MyData[i,], xlim=c(MyData[i,]$xmin, MyData[i,]$xmax), fun=function(x){MyData[i,]$a0+MyData[i,]$a1*x} )
Well, it failed too.
I may try to write manually, line by line for every combination, something like:
stat_function(data = MyData[(MyData$Species=="dog"),], fun=function(x){0.02540 + 0.03751 *x}, xlim=c(0.9334873, 1.0629578), colour="black", size=1, linetype=6)
But 1) I am not even sure it will work and 2) my real dataset has 3 species, 3 organs, 5 to 7 stages and 57 lines, and it will be tedious to do it. And I am running out of idea how to write this piece of code.
That's why I am calling for help. Any idea to do what I want? Thank you by advance for the help!
geom_point()
and add+geom_smooth(method = 'lm')
. This will plot your regression lines without pre-computation. Makes your life much easier. you can also only plot your regression lines, without plotting the real data points – tjebo