I'm trying to use a combination of this answer for annotating equations onto a ggplot
plot and this answer of putting different texts onto different facets.
The problem I'm getting is that I can't get different formulas using mathematical expressions onto different facets.
#Required package
library(ggplot2)
#Split the mtcars dataset by the number of cylinders in each engine
cars.split <- split(mtcars, mtcars$cyl)
#Create a linear model to get the equation for the line for each cylinder
cars.mod <- lapply(cars.split, function(x){
lm(wt ~ mpg, data = x)
})
#Create predicted data set to add a 'geom_line()' in ggplot2
cars.pred <- as.data.frame(do.call(rbind,
mapply(x = cars.split, y = cars.mod,
FUN = function(x, y){
newdata <- data.frame(mpg = seq(min(x$mpg),
max(x$mpg),
length.out = 100))
pred <- data.frame(wt = predict(y, newdata),
mpg = newdata$mpg)
}, SIMPLIFY = F)))
cars.pred$cyl <- rep(c(4,6,8), each = 100)
(cars.coef <- as.data.frame(do.call(rbind, lapply(cars.mod, function(x)x$coefficients))))
#Create a data frame of line equations a 'cyl' variable to facilitate facetting
#as per second link. I had to MANUALLY take the values 'cars.coef' and put them
#into the data frame.
equation.text <- data.frame(label = c('y = 4.69-0.09x^{1}',
'y = 6.42-0.17x^{1}',
'y = 6.91-0.19x^{1}'),
cyl = c(4,6,8))
#Plot it
ggplot(data = mtcars, mapping = aes(x = mpg, y = wt)) +
geom_point() +
geom_line(data = cars.pred, mapping = aes(x = mpg, y = wt)) +
geom_text(data = equation.text, mapping = aes(x = 20, y = 5, label = label)) +
facet_wrap(.~ cyl)
The equation in the plot is exactly as I had written in the equation.text
data frame, which is no surprise since the equations are in ''
. But I'm trying to get it to be in mathematical notation, like $y = 4.69–0.09x^1$
I know I need to use expression
as it said in the first link I had, but when I try to put it into a data frame:
equation.text <- data.frame(label = c(expression(y==4.69-0.9*x^{1}),
expression(y==6.42-0.17*x^{1}),
expression(y==6.91-0.19*x^{1})),
cyl = c(4,6,8))
I get an error saying expression
s can't be put into data frames:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class '"expression"' to a data.frame
My questions are:
- How can I get different equations in mathematical notation (italicized letters, superscripts, subscripts) in different facets?
- What's a more automated way of getting values from the
cars.coef
data frame into theequations
table (rather than typing out all the numbers!)? - UPDATE: This has been brought to my attention, but a lot of the answers seem to work for linear models. Is there a way to do it for, say, a non-linear model as well?