How To Draw A Line Graphiin R
- 1 Drawing a line chart in R with the plot function
- ane.ane Line plot types
- 1.2 Adding text to the plot
- 2 The curve function
- 3 Line graph in R with multiple lines
- 3.1 The matplot and matlines functions
- 3.2 Line chart with categorical data
- 3.3 Line chart fable
- iv Line chart in R with two axes (dual axis)
Drawing a line chart in R with the plot role
A line chart tin be created in base R with the plot function. Consider that you take the data displayed on the tabular array below:
| ten | y |
|---|---|
| one | 200 |
| 2 | 400 |
| iii | 600 |
| iv | 700 |
| 5 | 500 |
You lot can plot the previous data using three dissimilar methods: specifying the two vectors, passing the information as information frame or with a formula. Note that nosotros set type = "l" to connect the information points with straight segments.
# Data x <- c(one, two, 3, 4, 5) y <- c(200, 300, 600, 700, 500) # Vectors plot(ten, y, blazon = "l") # Data frame plot(information.frame(x, y), type = "l") # Equivalent # Formula plot(y ~ x, blazon = "l") # Equivalent
The manner of the line graphs in R tin be customized with the arguments of the function. Every bit an example, the color and line width tin exist modified using the col and lwd arguments, respectively.
plot(x, y, type = "l", col = "lightblue", # Color lwd = 5) # Line width
Line plot types
Too type = "l", there are three more types of line graphs available in base R. Setting blazon = "south" will create a stairs line graph, type = "b" will create a line plot with segments and points and type = "o" will also brandish segments and points, merely with the line overplotted.
par(mfrow = c(1, three)) plot(ten, y, blazon = "south", main = 'type = "southward"') plot(x, y, blazon = "b", main = 'type = "b"') plot(ten, y, type = "o", main = 'type = "o"') par(mfrow = c(1, i))
Furthermore, at that place be vi unlike types of lines, that can be specified making apply of the lty argument, from 1 to half-dozen:
par(mfrow = c(ii, 3)) plot(x, y, type = "l", lwd = 2, lty = ane, main = "lty = 1") plot(x, y, type = "fifty", lwd = 2, lty = two, main = "lty = two") plot(x, y, blazon = "50", lwd = ii, lty = 3, main = "lty = iii") plot(x, y, type = "l", lwd = 2, lty = 4, primary = "lty = iv") plot(x, y, type = "l", lwd = 2, lty = five, chief = "lty = 5") plot(x, y, type = "fifty", lwd = 2, lty = vi, primary = "lty = 6") par(mfrow = c(1, 1))
You can also customize the symbol used when type = "b" or type = "o". These symbols, too known as pch symbols can be selected with the pch argument, that takes values from 0 (square) to 25. See pch symbols for more information. Some of the bachelor symbols are the post-obit:
The colour of the symbol can be specified with the col argument, that will likewise alter the color of the line.
plot(x, y, type = "b", cex = two, pch = 21, bg = "blue", col = "red")
Symbols from 21 to 25 can exist specified with a background colour (different from the border), making employ of the bg argument.
Yet, you tin can also add the points separately using the points office. This approach will allow you to customize all the colors every bit desired.
plot(x, y, type = "50", col = "blood-red") # Calculation points points(ten, y, # Coordinates pch = 21, # Symbol cex = 2, # Size of the symbol bg = "green", # Background color of the symbol col = "blue", # Edge colour of the symbol lwd = iii) # Edge width of the symbol
Note that the pch argument besides let to input characters, but but one. In the following example we are passing the first v letters of the alphabet.
plot(x, y, type = "b", pch = LETTERS[1:5], # Letters as symbols cex = 2, # Size of the symbols col = i:5, # pch colors xlim = c(0, 6), # X-axis limits ylim = c(150, 750)) # Y-axis limits
Adding text to the plot
In instance yous need to make some annotations to the chart you can apply the text function, which commencement argument is the 10 coordinate, the second the Y coordinate and the tertiary the notation.
plot(x, y, type = "fifty") text(x = 3, y = 650, "Custom note")
Yous can also specify a characterization for each betoken, passing a vector of labels.
labels <- c("Text 1", "Text 2", "Text 3", "Text iv", "Text 5") plot(ten, y, type = "l", xlim = c(0.5, 5.5), # X-axis limit ylim = c(150, 750)) # Y-axis limit text(x = x, y = y, labels, col = "red")
The curve function
In the previous section we reviewed how to create a line chart from 2 vectors, but in some scenarios you volition need to create a line plot of a role. For that purpose you can use the curve function, specifying the office and the X-axis range with the arguments from and to.
bend(cos, from = 0, to = ten, ylab = "", main = "Sine and cosine") # New bend over the kickoff bend(sin, from = 0, to = 10, col = 2, add = TRUE) # Needed to add together the curve over the first
Note that you tin besides create a line plot from a custom role:
# Custom office fun <- office(10){ render(x ^ 3) } # Plot the custom part curve(fun, from = -5, to = five, ylab = expression(x^3), main = "bend office")
Line graph in R with multiple lines
If you have more variables you tin can add together them to the same plot with the lines office. As an case, if you have other variable named y2, you lot tin create a line graph with the two variables with the following R code:
# More than information y2 <- c(300, 400, 450, 400, 250) # First line plot(10, y, type = "l") # 2d line lines(x, y2, blazon = "l", col = two) # Same X values
Notation that the lines function is not designed to create a plot by itself, but to add a new layer over a already created plot.
The matplot and matlines functions
A better approach when dealing with multiple variables inside a information frame or a matrix is the matplot function. Because that you accept the following multivariate normal data:
# install.packages("MASS") library(MASS) # For the mvrnorm part set.seed(1) # Multivariate normal data means <- rep(0, 5) variances <- matrix(i:25, ncol = 5) data <- data.frame(mvrnorm(northward = 10, mu = means, Sigma = variances)) # First six rows head(information) X1 X2 X3 X4 X5 1 0.9290410 -one.5584821 ane.6540593 2.65356974 4.6452049 ii -0.1720333 -i.4431276 -0.8738552 -0.06321522 -0.8601666 3 0.6801899 ii.2411593 3.7697473 3.34137647 3.4009497 4 -1.8517645 0.4274748 -iii.5673172 -8.44912188 -ix.2588224 5 -0.1966158 -1.7617016 -3.0887668 -0.01224664 -0.9830791 six 0.7674637 2.1241256 2.4990073 iii.68081631 3.8373183 You tin can plot all the columns at one time with the function:
# Plot all columns at one time matplot(data, type = "50", principal = "matplot function")
Equivalently to the lines function, matlines allows adding new lines to an existing plot. For instance, you can plot the kickoff three columns of the data frame with the matplot function then add the concluding ii with matlines.
# Three first columns of the data frame data1 <- data[, 1:3] # Plot the three columns at once matplot(data1, type = "l", lty = 1, ylab = "data", ylim = c(min(data), max(information))) # Y-axis limits
# Two last columns of the data frame data2 <- data[, 4:5] # Add the data to the previous plot matlines(data2, type = "50", lty = ane, col = iv:5)
Line chart with categorical data
In addition to creating line charts with numerical data, it is also possible to create them with a categorical variable. Consider the following sample information:
# Data information <- information.frame(group = as.factor(c("Group 1", "Group 2", "Grouping 3")), var1 = c(1, 3, 2), var2 = c(2, ane.5, ane.75)) head(data) group var1 var2 1 Group 1 i two.00 two Grouping 2 three 1.50 iii Group 3 2 1.75 If you want to plot the information equally a line graph in R you can transform the factor variable into numeric with the is.numeric function and create the plot. You can set the cistron variable on the X-centrality or on the Y-centrality:
par(mfrow = c(1, ii)) #----------------- # Groups on X-centrality #----------------- plot(as.numeric(information$grouping), data$var1, type = "l", ylab = "Value", xlab = "Group", xaxt = "northward") # Second variable lines(as.numeric(data$grouping), data$var2, col = two) # Group names axis(1, labels = as.character(data$group), at = as.numeric(information$group)) #----------------- # Groups on Y-axis #----------------- plot(data$var1, as.numeric(data$group), type = "50", ylab = "Group", xlab = "Value", yaxt = "northward") # Second variable lines(information$var2, every bit.numeric(data$group), col = 2) # Grouping names axis(2, labels = equally.character(data$group), at = as.numeric(data$group)) par(mfrow = c(one, 1))
Line nautical chart legend
The fable function allows adding legends in base R plots. You only need to specify the position or the coordinates, the labels of the fable, the line type and the colour. You can also specify a pch symbol if needed.
plot(x = 1:x, y = one:10, type = "l") lines(x = 1:ten, y = sqrt(1:x), col = 2, type = "fifty") legend("topleft", legend = c("line 1", "line2"), lty = i, col = 1:2)
Line nautical chart in R with two axes (dual axis)
Finally, it is important to note that y'all can add a second centrality with the centrality function as follows:
# Increase the plot margins par(mar = c(5.25, 4.25, 4.25, 4.25)) # Commencement line plot(x = 1:x, y = ane:10, type = "l", xlab = "x", ylab = "Line 1") # New plot (needed to merge both plots) par(new = Truthful) # 2nd line plot(1:x, (1:10)^ii, type = "l", col = 2, axes = FALSE, # No axes bty = "n", # No box xlab = "", ylab = "") # New centrality centrality(4) mtext("Line 2", side = four, line = 3, col = ii)
Source: https://r-coder.com/line-graph-r/
Posted by: eppsreck1993.blogspot.com

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