R language drawing-add mathematical expressions and R2 (Ⅱ)
Preface
I didn't expect the handwritten blog to get so much attention!
The number of visits in one day directly exceeded a thousand, and it was encouraged by the great god! (excitement)
It is estimated that everyone is attracted by this title. (Haha)
Sure enough, Tianxiaku R has been adding mathematical expressions for a long time .
However, there are still many details in the second blog that are not perfect. This time, the code and detailed explanation are attached.
In the future, badges will share some tips for drawing pictures in ordinary times. Friends who like R language are welcome to communicate.
Then we go directly to the topic!
text
We generally use the R geom_smooth()
to add a series of smooth curve function.
But the methods given in some reference books are really limited, and they are not easy to use.
The badge was also uncomfortable when I first used R for drawing, and I looked for the code for a long time.
This time, directly use the stat_function()
function to complete the drawing.
First, give the code:
a <- c(1, 2, 3, 4, 5)
b <- c(5, 4, 3, 2, 1)
c <- data.frame(a, b)
library(ggplot2)
p <- ggplot(data = c, aes(x = a, y = b)) +
geom_point() +
geom_smooth(method = "lm", color = "red")
p
Figure 1 is as follows:
through fitting, the function isy = -x + 6 , R^2 = 1
We can stat_function()
direct curve fitting function.
code show as below:
p <- ggplot(data = c, aes(x = a, y = b)) +
geom_point() +
stat_function(fun = function(x)-1*x+6, color = "orange", size = 1)
p
Figure 2 is as follows:
Finally, use the annotate()
function to add text.
p <- ggplot(data = c, aes(x = a, y = b)) +
geom_point() +
stat_function(fun = function(x)-1*x+6, color = "orange", size = 1) +
annotate("text", x = 4, y = 5, parse = TRUE,
label = "y == -x+6", size = 4) +
annotate("text", x = 4, y = 4.5, parse = TRUE,
label = "atop(R^2==1)", size = 4)
p
Figure 3 is as follows:
Supplement 1:Subscript code
In Figure 3, we also used the superscript code, atop()
function.
At the same time, the code of the subscript is also given here.
p <- ggplot(data = c, aes(x = a, y = b)) +
geom_point() +
stat_function(fun = function(x)(-1)*x+6, color = "orange", size = 1) +
annotate("text", x = 4, y = 5, parse = TRUE,
label = "y == -x+6", size = 4) +
annotate("text", x = 4, y = 4.5, parse = TRUE,
label = "atop(R^2==1)", size = 4) +
annotate("text", x = 4, y = 4, parse = TRUE, #下标
label = "x[1]==x[2]", size = 4)
p
As shown:
Supplement 2 Other functions
If it is other functions, write the code directly like this:
#指数函数
stat_function(fun = function(x)1.5015*exp(x*(-0.009)), size = 1) +
annotate("text", x = 25, y = 2.5, parse = TRUE, label = "y == 1.5015*e^{-0.009*x}")
#二次函数:
stat_function(fun = function(x)-90.613*x+630.42*x^2+4.3696, size = 1) +
annotate("text", x = 0.09, y = 2.51, parse = TRUE, label = "y == 630.42*x^{2}-90.613*x+4.3696")
#注意:这里的上标使用{
}表达
If you don’t understand, you are welcome to discuss it in the comment area. Please also criticize and correct.