R Line Graphs

R Line Graphs

In this guide, we will discuss R Line Graphs.

A line graph is a pictorial representation of information that changes continuously over time. A line graph can also be referred to as a line chart. Within a line graph, there are points connecting the data to show the continuous change. The lines in a line graph can move up and down based on the data. We can use a line graph to compare different events, information, and situations.

A line chart is used to connect a series of points by drawing line segments between them. Line charts are used in identifying the trends in data. For line graph construction, R provides plot() function, which has the following syntax:

plot(v,type,col,xlab,ylab)  

Here,

S.NoParameterDescription
1.vIt is a vector that contains numeric values.
2.typeDoes this parameter take the value? I? to draw only the lines or ?p? to draw only the points and “o” to draw both lines and points.
3.labIt is the label for the x-axis.
4.labIt is the label for the y-axis.
5.mainIt is the title of the chart.
6.colIt is used to give the color for both the points and lines

Let’s see a basic example to understand how to plot() function is used to create the line graph:

Example

# Creating the data for the chart.  
v <- c(13,22,28,7,31)  
# Giving a name to the chart file.  
png(file = "line_graph.jpg")  
# Plotting the bar chart.   
plot(v,type = "o")  
# Saving the file.  
dev.off()  

Output:

R Line Graphs

Line Chart Title, Color, and Labels

Like other graphs and charts, inline chart, we can add more features by adding more parameters. We can add the colors to the lines and points, add labels to the axis, and can give a title to the chart. Let’s see an example to understand how these parameters are used in plot() function to create an attractive line graph.

Example

# Creating the data for the chart.  
v <- c(13,22,28,7,31)  
# Giving a name to the chart file.  
png(file = "line_graph_feature.jpg")  
# Plotting the bar chart.   
plot(v,type = "o",col="green",xlab="Month",ylab="Temperature")  
# Saving the file.  
dev.off()  

Output:

R Line Graphs

Line Charts Containing Multiple Lines

In our previous examples, we created line graphs containing only one line in each graph. R allows us to create a line graph containing multiple lines. R provides lines() function to create a line in the line graph.

The lines() function takes an additional input vector for creating a line. Let?s see an example to understand how this function is used:

Example

# Creating the data for the chart.  
v <- c(13,22,28,7,31)  
w <- c(11,13,32,6,35)  
x <- c(12,22,15,34,35)  
# Giving a name to the chart file.  
png(file = "multi_line_graph.jpg")  
# Plotting the bar chart.   
plot(v,type = "o",col="green",xlab="Month",ylab="Temperature")  
lines(w, type = "o", col = "red")  
lines(x, type = "o", col = "blue")  
# Saving the file.  
dev.off()  

Output

R Line Graphs

Line Graph using ggplot2

In R, there is another way to create a line graph i.e. the use of ggplot2 packages. The ggplot2 package provides geom_line(), geom_step() and geom_path() function to create line graph. To use these functions, we first have to install the ggplot2 package and then we load it into the current working library.

Let?s see an example to understand how ggplot2 is used to create a line graph. In the below example, we will use the predefined ToothGrowth dataset, which describes the effect of vitamin C on tooth growth in Guinea pigs.

Example

library(ggplot2)  
#Creating data for the graph  
data_frame<- data.frame(dose=c("D0.5", "D1", "D2"),  
len=c(4.2, 10, 29.5))  
head(data_frame)  
png(file = "multi_line_graph2.jpg")  
# Basic line plot with points  
ggplot(data=data_frame, aes(x=dose, y=len, group=1)) +geom_line()+geom_point()  
# Change the line type  
ggplot(data=df, aes(x=dose, y=len, group=1)) +geom_line(linetype = "dashed")+geom_point()  
# Change the color  
ggplot(data=df, aes(x=dose, y=len, group=1)) +geom_line(color="red")+geom_point()  
dev.off()  

Output

R Line Graphs

Next Topic; Click Here

This Post Has One Comment

Leave a Reply