YouTip LogoYouTip

R Line_Charts

# R Plotting - Function Curve Graph A function curve graph is an important tool for studying functions. The `curve()` function in R can be used to plot the graph of a function. The code format is as follows: ```r curve(expr, from = NULL, to = NULL, n = 101, add = FALSE, type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL, xlim = NULL, …) # S3 method for plot plot(x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL, …) **Note:** R language has S3 and S4 classes. S3 classes are more widely used; they are simple and crude to create but flexible, while S4 classes are more refined. Parameters: * `expr`: The function expression. * `from` and `to`: The start and end range for plotting. * `n`: An integer value representing the number of x values. * `add`: A logical value. When TRUE, it indicates that the plot should be added to an existing plot. * `type`: The type of plot. `p` for points, `l` for lines, `o` for both points and lines with lines passing through points. * `xname`: The name for the x-axis variable. * `xlim` and `ylim`: Represent the ranges for the x-axis and y-axis. * `xlab`, `ylab`: The label names for the x-axis and y-axis. In the `plot` function, `x` and `y` represent the horizontal and vertical coordinates of the graph, respectively. Below, we plot a graph of the sin(x) function: ```r curve(sin(x), -2 * pi, 2 * pi) !(#) Note: Any computer plotting tool creates a schematic diagram; it cannot guarantee an exact match with the true function graph. It simply samples points at certain intervals, calculates the "height" of each point, and plots it. To ensure curve continuity, straight lines connect adjacent points. Therefore, in some cases, such as with tan(x), errors may occur: !(#) Breakpoints occur at every (2n+1)Pi / 2 position, but R's graph connects them. Please understand this point. Of course, not all functions support vector processing like sin does. We can also manually generate a number sequence and then use the `plot` function to create the function graph. Suppose function `f` only accepts a single numerical value as an argument: ## Example ```r # Define function f f = 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 <- f(i) j <- j + 1 } # Plot the graph plot(x, y, type='l') !(#) Next, we use the `plot()` function to plot vector data: ## Example ```r # Vector data v <- c(7, 12, 28, 3, 41) # Generate image png(file="line_chart_label_colored.jpg") # Plot, line color is red, main parameter sets the title plot(v, type="o", col="red", xlab="Month", ylab="Rain fall", main="Rain fall chart") !(#) AI is thinking... [](#)[R Plotting – Chinese Support](#) [R Plotting – Scatterplots](#)[](#)
← Python3 Func Number PowPython3 Func Number Max β†’