How to Plot Multiple Lines in R

Plot multiple line graphs on the same window with auto-assigned different colors

The function to use is matplot, not plot. There is also matlines but if the data to be plotted is in a matrix, matplot can plot all columns in one call.

  1. Create a vector of y coordinates, yy, from the x values. This is done in a sapply loop. In the code below I have called the x coordinates values xx since there is no dat[,2] to work with.
  2. Plot the resulting matrix in one matplot function call, which takes care of the colors automatically.

The lines labels problem is not addressed, only the lines plotting problem. With so many lines their labels would make the plot more difficult to read.

icc <- function(year, x) {
z = exp(year - x)
inf = z / (1 + z)
return (inf)
}

# Example data
year <- seq(-4, 4, 0.1)

xx <- seq(-1, 1, by = 0.2)
yy <- sapply(xx, \(x) icc(year, x))
matplot(year, yy, type = "l", lty = "solid")

Sample Image

Created on 2022-07-26 by the reprex package (v2.0.1)



Note

Function icc is the logistic distribution CDF with location x and scale 1. The base R plogis function can substitute for it, the results are equal within floating-point precision.

icc2 <- function(year, x) plogis(year, location = x, scale = 1)

yy2 <- sapply(xx, \(x) icc2(year, x))
identical(yy, yy2)
#> [1] FALSE
all.equal(yy, yy2)
#> [1] TRUE

How to plot multiple line chart in ggplot2?

The quick way is adding two geom_lines, one for each column - as @Basti and @Yacine Hajji have suggested. However, this is suboptimal (for example, it won't create a legend).

The better way is reshaping your dataset to long format, as @mfalco is suggesting. This can be done programmatically, for example with tidyr's pivot_longer.

# Create dataset
dat <- data.table::fread("Date Total Count_1 %_one Count_2 %_two
07-11-2022 368 43 11.7% 132 35.9%
07-12-2022 510 56 11.0% 177 34.7%
07-13-2022 544 62 11.4% 187 34.4%
07-14-2022 496 77 15.5% 196 39.5%
07-15-2022 320 39 12.2% 118 36.9%
07-16-2022 295 33 11.2% 99 33.6%")

library(tidyr)
library(dplyr)
library(ggplot2)

dat_long <- dat |>
# Make names follow the same pattern
rename(percent_1 = `%_one`, percent_2 = `%_two`) |>
# Reshape multiple columns at once
pivot_longer(-c(Date, Total),
names_to = c(".value", "measurement"),
names_pattern = "(.*)_(.*)"
) |>
# Coerce data type for the percent column from chr to numeric
mutate(percent = gsub("%", "", percent) |> as.numeric())

# Plot
dat_long |>
ggplot(aes(Date, percent, colour = measurement, group = measurement)) +
geom_line() +
coord_cartesian(ylim = c(0, 50))

Sample Image

Created on 2022-08-12 by the reprex package (v2.0.1)

How to plot a multiple line graph in R

It's very difficult to help without a reproducible example, but one way would be to use ggplot2. You can use either the group or the color aesthetic in your plot:

library(coronavirus)
library(dplyr)
library(ggplot2)
data(coronavirus)

coronavirus %>%
filter(type == "confirmed") %>%
filter(Country.Region != "Mainland China") %>%
group_by(Country.Region, date) %>%
summarise(total = sum(cases)) %>%
ggplot(aes(x = date, y = total, color = Country.Region)) +
geom_line()

Plot two graphs in same plot in R

lines() or points() will add to the existing graph, but will not create a new window. So you'd need to do

plot(x,y1,type="l",col="red")
lines(x,y2,col="green")


Related Topics



Leave a reply



Submit