English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Function curve graphs are an important tool for studying functions.
The curve() function in R can draw the image of a function, and the code format is as follows:
curve(expr, from = NULL, to = NULL, n = 101, add = FALSE, type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL, xlim = NULL, ...) # S3 function methods plot(x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL, ...)
Note:R language classes have S3 class and S4 class, S3 The class is widely used, creating simple and rough but flexible, while S4 The class is relatively refined.
Parameters:
expr: the function expression.
from and to: the start and end range of the plot.
n: an integer value representing the number of values x takes.
add: a logical value, indicating that the plot is added to an existing plot when TRUE.
type: the type of plotting, p for point, l for line, o to draw both points and lines, with the line passing through the points.
xname: the name used for the x-axis variable.
xlim and ylim represent the range of the x-axis and y-axis.
xlab, ylab: the label names of the x-axis and y-axis.
In the plot function, x and y represent the abscissa and ordinate of the drawn figure, respectively.
Here we draw a chart of the sin(x) function:
curve(sin(x)) -2 * pi, 2 * )pi
Note: Any computer drawing tool draws pattern charts, which cannot guarantee that they are exactly the same as the true function images. It only takes a point at a certain distance, then calculates the "height" of this point and draws it out. To ensure the continuity of the curve, there will be a straight line connecting adjacent points, so in some cases such as tan(x), errors may occur:
at each (2n+1)Pi / 2 locations will appear breakpoints, but R's images connect them, I hope everyone understands this point.
Of course, not all functions support vector processing like sin, we can also manually generate a number sequence and then use the plot function to generate a function image. Assuming the function f only supports a single number as a parameter:
# Define function f f = function(x) { if (x >= 0) { x } else { x ^ 2 } } # Generate independent variable sequence x = seq(-2, 2, length=100) # Generate dependent variable sequence y = rep(0, length(x)) j = 1 for (i in x) { y[j] = f(i) j = j + 1 } # Draw the image plot(x, y, type='l')
Next, we use the plot() function to plot vector data:
# Vector data v <- c(7,12,28,3,41) # Generate image png(file = "line_chart_label_colored.jpg") # Plotting, line chart color is red, the main parameter is used to set the title plot(v, type = "o", col = "red", xlab = "Month", ylab = "Rain fall" main = "Rain fall chart"