R Language –Presentation of Data : Online Course
R line graphs
- Function plot() is the main tool for 2D plotting in R.
- If we have two vectors x, y of equal length, function plot(x,y) allows us to get the graph of y(x) dependence.
- The function plot(x,y) takes parameters for specifying points in the diagram.
- Parameter 1 specifies points on the x-axis.
- Parameter 2 specifies points on the y-axis.
- If only one variable is given, we will get a graph for which set vector provide the ordinate values and the points ordinal numbers are abscissas.
- A line graph has a line that connects all the points in a diagram.
- If you want to create line it is required plot function and type is “l”.
Some arguments of the plot() function:
Following arguments in details.
S.NO | PARAMETER | DESCRIPTION |
---|---|---|
1. | v | It is a vector which contains the numeric values. x is specifies points on the x-axis. y is specifies points on the y-axis. |
2. | type | change the plot type. It accepts the following strings and has the given effect.”p” – points “l” – lines “b” – both points and lines “c” – empty points joined by lines “o” – overplotted points and lines “s” and “S” – stair steps “h” – histogram-like vertical lines “n” – does not produce any points or lines |
3. | xlab | It is the label for the x-axis. |
4. | ylab | It is the label for the y-axis. |
5. | main | It is the title of the chart. |
6. | col | It is used to give the color for both the points and lines |
7 | pch | pch is used to change the point shape format. |
8 | cex | Use cex=number to change the size of the points |
9 | Legend() | Legend takes as input the coordinates, text and the symbols to be interpreted. |
10 | Ity | lty is the text sequence of four numbers from 1 to 9, for example: lty = “4241”. |
Example : Single Line
Input: To draw a single line using type= “l” parameter |
plot(1:10, main=”My Graph”, xlab=”The x-axis”, ylab=”The y axis”, type=”l”) |
Output: |
Example : Color and Line Width
Input: To draw line width and Colors using “col” and “lwd” parameter |
plot(1:10, main=”My Graph”, xlab=”The x-axis”, ylab=”The y axis”, type=”l”, col=”red”, lwd = 2) |
Output: |
Color lines and Line Style
Input: The lines have been colored “col” and line style “lty” by the parameter of the plot() function. |
vec = c(12,45,11,58,74) plot(vec, type = ‘l’, col = ‘green’, lty=1) |
Output: |
Available parameter values for lty
:
0
removes the line1
displays a solid line2
displays a dashed line3
displays a dotted line4
displays a “dot dashed” line5
displays a “long dashed” line6
displays a “two dashed” line
Multiple line graphs on single plot
Input: To Draw multiple line on single plot |
vec1 = c(12,45,11,58,74) vec2 = c(45,12,1,87,10) vec3 = c(10,4,15,56,25) plot(vec1, type = ‘o’, col = ‘red’, xlab = ‘X-Axis’, ylab = ‘Y-Axis’, main = ‘Line Graph’, ylim = c(0,100)) lines(vec2, type = “o”, col = “green”) lines(vec3, type = “o”, col = “purple”) |
Output: |