To create line chart in R, you can use plot() function or ggplot() function from ggplot2 package.

The following methods show how you can do it with syntax.

Method 1: Use plot() Function

plot(x, y, type = "l", col = "blue", lwd = 2, xlab = "X-axis label", ylab = "Y-axis label", main = "My Line Chart")

Method 2: Use ggplot() Function

ggplot(df, aes(x, Vy))+geom_line()+xlab(label='x')+ylab(label='y')

The following examples show to create line chart in R.

Create a Line Chart Using plot() Function in R

Let’s see how to plot() function in R:

# Sample data
x <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
y <- c(2, 4, 6, 9, 12, 15, 16, 13, 7, 5)

# Build the line chart
plot(x, y, type = "l", col = "blue", lwd = 2, xlab = "X-axis label", ylab = "Y-axis label", main = "My Line Chart")

Output:

Line chart

Here the above snippet shows line chart for the data that we declared in above code.

Create a Line Chart Using ggplot() Function in R

library(ggplot2)

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26")),
                 Machine_name = c("Machine1","Machine2","Machine1","Machine3","Machine2","Machine3"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Plot line chart
ggplot(df, aes(Start_date, Value,group=Machine_name, colour=Machine_name))+geom_line()+xlab(label='dates')+ylab(label='value')

Output:

Line chart

Using above code we can plot line chart for time series data.