How To Add A Title To A Scatter Plot In R
R Plot Function
In this article, you'll acquire to use plot function in R. Information technology is used to make graphs according to the blazon of the object passed.
The most used plotting function in R programming is the
plot()
office. It is a generic role, meaning, it has many methods which are called co-ordinate to the blazon of object passed to plot()
. In the simplest case, nosotros can laissez passer in a vector and we will get a besprinkle plot of magnitude vs index. But generally, we pass in 2 vectors and a scatter plot of these points are plotted.
For example, the command plot(c(one,two),c(3,five))
would plot the points (i,3)
and (2,5)
.
Hither is a more concrete example where we plot a sine office form range -pi
to pi
.
x <- seq(-pi,pi,0.1) plot(x, sin(x))
Adding Titles and Labeling Axes
Nosotros tin can add together a title to our plot with the parameter chief
. Similarly, xlab
and ylab
can exist used to characterization the 10-axis and y-axis respectively.
plot(10, sin(x), main="The Sine Part", ylab="sin(ten)")
Changing Color and Plot Type
We can see above that the plot is of round points and black in color. This is the default color.
We can change the plot blazon with the argument blazon
. It accepts the following strings and has the given upshot.
"p" - points "l" - lines "b" - both points and lines "c" - empty points joined by lines "o" - overplotted points and lines "southward" and "S" - stair steps "h" - histogram-like vertical lines "northward" - does not produce any points or lines
Similarly, we can define the color using col
.
plot(x, sin(x), main="The Sine Function", ylab="sin(x)", type="l", col="blue")
Overlaying Plots Using legend() part
Calling plot()
multiple times will accept the effect of plotting the current graph on the same window replacing the previous ane.
Yet, sometimes we wish to overlay the plots in order to compare the results.
This is made possible with the functions lines()
and points()
to add lines and points respectively, to the existing plot.
plot(x, sin(x), chief="Overlaying Graphs", ylab="", type="l", col="blue") lines(x,cos(x), col="carmine") legend("topleft", c("sin(x)","cos(x)"), fill=c("blue","red") )
We have used the function fable()
to appropriately brandish the legend. Visit legend() function to learn more.
As well visit plot() function to learn more than about dissimilar arguments plot()
function can accept, and more examples.
How To Add A Title To A Scatter Plot In R,
Source: https://www.datamentor.io/r-programming/plot-function/
Posted by: wyattblem1987.blogspot.com
0 Response to "How To Add A Title To A Scatter Plot In R"
Post a Comment