How To Draw Diagonal Line In Plot In R
Base R provides several functions to add together segments and arrows to the plots. In this guide nosotros are going to use the following sample plot:
# Sample data set.seed(132) ten <- seq(1, 10, by = 0.05) y <- x ^ 2 + rnorm(x, sd = 10) # Plotting part used in the examples beneath splot <- office(x, y) { plot(x, y, pch = ifelse(10 > 8, 23, ifelse(y < 0, 24, 21)), bg = ifelse(ten > 8, "deepskyblue", ifelse(y < 0 , "orange", "lightgreen"))) } The abline function
The abline function allows cartoon lines, such as horizontal lines (h statement), vertical lines (v argument), lines based on a intersection and a slope (a and b arguments) or for plotting a regression line.
Horizontal line
The h argument allows you lot to set up the Y-centrality value where to draw a horizontal line.
splot(x, y) # Sample role # Horizontal line at Y = 0 abline(h = 0)
Vertical line
The v statement allows you to fix the Ten-axis values where to draw vertical lines.
splot(x, y) # Vertical line at X = 8 abline(v = 8)
Horizontal and vertical line at the same fourth dimension
You can also add vertical and horizontal lines at the same time specifying both arguments.
splot(x, y) # Horizontal line at Y = 1 and vertical at X = 8 abline(h = 1, v = viii)
Intercept and slope
In addition to horizontal or vertical lines, you can also specify an intercept with the statement a and the gradient of the line with the argument b.
splot(x, y) abline(a = -xv, # Intercept b = 10) # Gradient
Regression line
The abline function also allows cartoon linear regression lines from a model.
splot(x, y) abline(lm(y ~ ten)) # Linear regression
Line customization
The aforementioned customization of lines in base R tin be applied to this and the other functions of this guide.
splot(x, y) abline(h = 0, v = 8, col = c("ruddy", "green"), lwd = two, lty = ii:3)
The segments office
This function is very similar to abline, just you can specify the starting and terminate points of the lines with x0, x1, y0 and y1 arguments.
splot(x, y) segments(x0 = 1, x1 = 3, y0 = 0, y1 = 0, lwd = two, col = "red") segments(x0 = eight, x1 = 8, y0 = xl, y1 = 100, lwd = 2, col = "orange") The arrows function
Yous tin can set up the arrows the same style equally segments, specifying the start and cease of the arrow on each axis.
splot(x, y) arrows(x0 = three, x1 = seven, y0 = 40, y1 = ninety)
In improver to modifying the color, width or line type, the arrows role too allows customizing the length and the angle of the arrowhead.
splot(x, y) arrows(x0 = three, x1 = 7, y0 = 40, y1 = 90, length = 0.1, angle = twenty)
Source: https://r-charts.com/base-r/segments/
Posted by: saltzimen1990.blogspot.com

0 Response to "How To Draw Diagonal Line In Plot In R"
Post a Comment