R Plot
Plot
Example
Draw one point in the diagram, at position (1) and position (3):
plot(1, 3)
To draw more points, use vectors:
Example
Draw two points in the diagram, one at position (1, 3) and one in position (4, 5):
plot(c(1, 4), c(3, 5)Multiple Points
Example
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
For better organization, when you have many values, it is better to use variables:
Example
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
Sequences of Points
If you want to draw dots in a sequence, on both the x-axis and the y-axis, use the : operator:
Example
plot(1:10)
Draw a Line
Example
plot(1:10, type="l")
Plot Labels
Example
plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")
Example:
plot(1:10, col="red")
Size
Example:
plot(1:10, cex=2)
Point Shape
Use pch with a value from 0 to 25 to change the point shape format:
Example
plot(1:10, pch=25, cex=2)
Plot Trigonometric Function in R
In R, we can also plot trigonometric functions.Let's generate a sine wave plot,
In the above example, we have generated a sine wave plot.We have used the seq() function to create the sequence vector x of values from -pi to pi with 0.1 interval. And assigned respective sine values of x to y.Finally, we plotted y against x using plot().
The plot() function is used to draw points (markers) in a diagram.The function takes parameters for specifying points in the diagram.
Parameter 1 specifies points on the x-axis.
Parameter 2 specifies points on the y-axis.
At its simplest, you can use the plot() function to plot two numbers against each other:
Example
Draw one point in the diagram, at position (1) and position (3):
plot(1, 3)
Output:
To draw more points, use vectors:
Example
Draw two points in the diagram, one at position (1, 3) and one in position (4, 5):
plot(c(1, 4), c(3, 5)Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in both axis:
Example
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
For better organization, when you have many values, it is better to use variables:
Example
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
If you want to draw dots in a sequence, on both the x-axis and the y-axis, use the : operator:
Example
plot(1:10)
Draw a Line
The plot() function also takes a type parameter with the value l to draw a line to connect all the points in the diagram:
Example
plot(1:10, type="l")
Plot Labels
The plot() function also accept other parameters, such as main, xlab and ylab if you want to customize the graph with a main title and different labels for the x and y-axis:
Example
plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")
Graph Appearance
There are many other parameters you can use to change the appearance of the points.
Colors
Use col="color" to add a color to the points:
plot(1:10, col="red")
Use cex=number to change the size of the points (1 is default, while 0.5 means 50% smaller, and 2means 100% larger):
Example:
plot(1:10, cex=2)
Use pch with a value from 0 to 25 to change the point shape format:
Example
plot(1:10, pch=25, cex=2)
Sample Program
plot(1:10,col='red',pch=2,cex=2)
Output:
In R, we can also plot trigonometric functions.Let's generate a sine wave plot,
# sequence vector of values from -pi to pi with 0.1 interval
x = seq(-pi,pi,0.1)
# respective sine value of x
y = sin(x)
# plot y against x
plot(x,y,col='red',type="l")
Output:
In the above example, we have generated a sine wave plot.We have used the seq() function to create the sequence vector x of values from -pi to pi with 0.1 interval. And assigned respective sine values of x to y.Finally, we plotted y against x using plot().
Note: Similarly, we can generate wave plots of other trigonometric functions.
Comments
Post a Comment