Table of Contents

R is one of the most robust engines in the world when it comes to plotting and manipulating databases. However, it’s also among the hardest to master due to a huge number of packages. We believe that the easiest way to start learning R is to figure out how to add a line to a plot in R.

**How To Add A Line To A Plot In R With lines()**

The first approach that we will introduce to you is a tool offered by R’s graphic package, the `lines()`

function. As the name suggests, its main job is to add lines to any plot that you want.

To use this function properly, you must always call the plot function first and construct your plot. It takes in two input arguments and starts mapping variables according to the input.

A rule of thumb is that the first argument denotes the plot’s coordinates on the x-axis, making it compulsory. On the other hand, the second argument specifies the y-axis, and it’s optional.

Only after the plot finishes its creation process should you call `lines()`

and start passing in coordinates. Failure to comply with this rule often leads to incorrect scaling, which we will discuss further down the line.

Here is a short example:

```
library(stats)
library(babynames)
library(dplyr)
plot(cars$speed, cars$dist, type = "l", col = "red",
main = "Title of the Plot",
xlab = "Speed (mph)",
ylab = "Stopping Distance (Feet)")
lines(cars$speed, cars$dist/4 , col = "green")
legend("topleft", c("line 1", "line 2"),
lty = c(1,1),
col = c("red", "green"))
```

**How To Manipulate Plots’ Lines In R With points()**

In a similar way to `lines()`

, `points()`

is also an offer from the R’s graphic package. It has only one responsibility, to draw a series of points on a plot.

With just a glance, one may suggest that there is practically no difference between this method and the one above and be correct. The biggest variation between these two is the default type, as they are both plot.xy’s wrappers.

```
library(stats)
library(babynames)
library(dplyr)
plot(cars$speed, cars$dist, type = "l", col = "red",
main = "Title of the Plot",
xlab = "Speed (mph)",
ylab = "Stopping Distance (Feet)")
points(cars$speed, cars$dist, col = "blue" )
lines(cars$speed, cars$dist/4 , col = "green")
points(cars$speed, cars$dist/4, col = "black" )
legend("topleft", c("line 1", "line 2"),
lty = c(1,1),
col = c("red", "green"))
```

**How To Manipulate Plots’ Lines In R – Correct Scaling**

As we mentioned above, failing to initialize `plot()`

before calling `lines()`

may lead to scaling issues. The reason for this problem lies in the difference in scale between the functions. As a result, you will get one line completely out-of-bound in comparison with the other.

```
library(stats)
library(babynames)
library(dplyr)
dat <- babynames %>%
filter(name %in% c("Alice")) %>% filter(sex=="F")
dat2 <- babynames %>%
filter(name %in% c("Mary")) %>% filter(sex=="F")
plot(dat$year, dat$n, type = "l", col = "blue",
main = "Women born with different names",
xlab = "Year",
ylab = "Number of babies born")
lines(dat2$year, dat2$n, col = "red")
```

A popular solution is to fix it in a manual way, reordering each line by hand. Its biggest advantage is that it’s simple, as you just need to change a few lines of code.

However, the complexity of this solution increases exponentially when your code gets longer. You will need to add in a lot of conditions as well as dynamic checking scripts if you want to ensure your y-axis’s maximum value is correct.

This is one of the standard errors in R.

**Conclusion**

With this guide on how to manipulate plots’ lines in R, plotting should no longer pose any problem for you anymore. As long as you can use the three solutions we provided in their most appropriate situation, you will breeze through all problems of this type.

What you do need to keep in mind, however, is that the correct sequence is vital to this operation. Otherwise, you will have to deal with out-of-bound lines.

## Leave a comment