How To Draw A Line In R Plot
The basic plot command
Imagine that in R, nosotros created a variable $t$ for time points and a variable $z$ that showed a quantity that is decaying in time.
> t=0:10 > z= exp(-t/2) The simplest R command to plot $z$ versus $t$ is
> plot(t,z)
Without any other arguments, R plots the information with circles and uses the variable names for the axis labels. The plot control accepts many arguments to alter the wait of the graph. Here, we use type="l" to plot a line rather than symbols, change the color to greenish, make the line width exist 5, specify different labels for the $10$ and $y$ axis, and add together a championship (with the principal argument).
> plot(t,z, type="l", col="greenish", lwd=5, xlab="time", ylab="concentration", main="Exponential disuse")
A line plot with multiple series
Imagine that you wanted to plot not just $z$ simply likewise a variable $westward$ that was increasing with time.
> w = 0.1*exp(t/three) One mode to plot separate lines for both $z$ and $w$ is to offset plot $z$ with the plot and then add a line for $westward$ with the lines control.
> plot(t,z, blazon="l", col="green", lwd=five, xlab="time", ylab="concentration") > lines(t, w, col="scarlet", lwd=2) > championship("Exponential growth and decay") > fable(ii,1,c("decay","growth"), lwd=c(v,2), col=c("green","red"), y.intersp=ane.5) The last two lines add a title (since information technology wasn't added with a main argument of the plot command) and a legend. The first 2 arguments to the legend command are its position, the side by side is the legend text, and the following two are just vectors of the same arguments of the plot and lines commands, as R requires you to specify them again for the legend. (The final y.intersp argument merely increases the vertical spacing of the legend.)
Detect that the range of the plot does not aggrandize to include all of the line plotted by the lines command. By default, the plot sets the axis limits to fit the information given information technology. If you tin manual specify the axis limits with the xlim or ylim arguments. Adding the argument ylim = range(west,z) will ensure that the $y$-axis limits include all the data from both $z$ and $w$. The following commands volition show all the data of $w$. These commands also show how to add both points as well as lines by specifying type="b". The symbols used for the points are specified by the pch (plotting graphic symbol) statement.
> plot(t,z, type="b", col="green", lwd=five, pch=xv, xlab="time", ylab="concentration", ylim=range(due west,z)) > lines(t, w, type="b", col="carmine", lwd=2, pch=19) > championship("Exponential growth and decay") > legend(0,2.8,c("decay","growth"), lwd=c(5,ii), col=c("light-green","red"), pch=c(15,19), y.intersp=i.5)
Source: https://mathinsight.org/plotting_line_graphs_in_r
Posted by: mondragontrionevered68.blogspot.com

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