R 在刻面ggplot上注释不同的方程式
我试着用答案的组合将方程注释到R 在刻面ggplot上注释不同的方程式,r,ggplot2,expression,R,Ggplot2,Expression,我试着用答案的组合将方程注释到ggplotplot上,并用答案将不同的文本放到不同的面上 #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 f
ggplot
plot上,并用答案将不同的文本放到不同的面上
#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)
我遇到的问题是,我不能用数学表达式在不同的方面得到不同的公式
#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)
我得到一个错误,说表达式
s不能放入数据帧:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class '"expression"' to a data.frame
我的问题是:
cars.coef
数据框中获取值到等式
表中(而不是键入所有数字!)的更自动化的方法是什么希望这能满足问题的两个部分。我也不擅长组合表达式 对于第一部分,您可以从截距和系数数据框中创建一个等式文本数据框,并根据需要对其进行格式化。我设置了
sprintf
,以匹配小数位数,并标记系数的符号
库(ggplot2)
#与上述准备工作相同
#重命名为仅具有标准列名
一个可再现问题的名称(cars.coef)+1清楚解释并显示研究成果!这可能解决不了任何问题,但是为什么在方程中有=
而不是=
。文本
?在我第二次尝试方程文本时,使用了=
,因为当在表达式中使用并作为注释添加到绘图中时,它只显示为“=”而不是“==”因为expression
会将函数中的任何内容(如果你知道你在做什么,我恐怕不知道)转换成一个公式作为数学符号。这可能会有帮助:你只是去做了而已!但有三个问题:1。函数如何知道将信息从cars.coef$intercept
和cars.coef$mpg
放置到%1.2f
和%1.2f
的位置?2.您如何使用%
?为什么在第一个例子中,%
在1.2f
前面,而在第二个例子中,它在+
前面?3.1.2f
是做什么的?好吧,对不起,愚蠢的问题,我现在检查?sprintf
!