Draw Multiple Graphs and Lines in Same Plot in R
Last Updated :
06 Jun, 2021
A visualization can sometimes make more sense when multiple graphs and line plots are combined into one plot. In this article, we will discuss how we can do the same in the R programming language.
Method 1: Using base R
Base R supports certain methods that can be used to generate the desired plot. In this example plot, a scatter plot, a line plot, and a bar graph in the same frame for the same data.
barplot() function is used to produce a barplot with appropriate parameters.
Syntax:
barplot(H, xlab, ylab, main, names.arg, col)
Parameters:
- H: This parameter is a vector or matrix containing numeric values which are used in bar chart.
- xlab: This parameter is the label for x axis in bar chart.
- ylab: This parameter is the label for y axis in bar chart.
- main: This parameter is the title of the bar chart.
- names.arg: This parameter is a vector of names appearing under each bar in bar chart.
- col: This parameter is used to give colors to the bars in the graph.
points() function in R Language is used to add a group of points of specified shapes, size and color to an existing plot.
Syntax: points(x, y, cex, pch, col)
Parameters:
x, y: Vector of coordinates
cex: size of points
pch: shape of points
col: color of points
lines() function in R Language is used to add lines of different types, color, and width to an existing plot.
Syntax: lines(x, y, col, lwd, lty)
Parameters:
x, y: Vector of coordinates
col: Color of line
lwd: Width of line
lty: Type of line
The idea is simple and straightforward. Methods to add different visualizations just needs to be added to the code one by one, the plot will interpret each function and draw the plot accordingly.
Example:
R
df<-data.frame(x = c("A","B","C","D","E","F","G"),
y = c(10,23,32,65,16,89,78))
barplot(df$y, xlab = df$x, col = "yellow")
points(df$x, df$y, type = "o",col = "blue")
lines(df$x, df$y)
Output:

Method 2: Using ggplot
ggplot is a library supported by R which visualization easier. This again can be used to combine multiple graphs into one. A plot is generalized using ggplot() function and then all plots are added to the same plot using + sign.
Here, geom_bar() is used to draw the bar plot, geom_line() is used to draw the line chart and geom_point() is used for scatter plot.
Example:
R
library(ggplot2)
df<-data.frame(x = c("A","B","C","D","E","F","G"),
y = c(10,23,32,65,16,89,78),)
ggplot(df, aes(x, y, group = 1))+
geom_bar(stat = "identity")+
geom_line(color = "green")+
geom_point(color = "blue")
Output:
Similar Reads
Plot Paired dot plot and box plot on same graph in R R Programming Language is used for statistical computing and graphics. R was first developed at the University of Auckland by two professors Ross Ihanka and Robert Gentleman Dot Plot The dot plot is a graphical representation of how one attribute varies with respect to another attribute. On the x-ax
7 min read
Draw Multiple Function Curves to Same Plot in R In this article, we will discuss how to plot multiple function curves to the same plot in the R programming language. Method 1: In Base R Base R supports a function curve() which can be used to visualize a required function curve. It supports various parameters to edit the curve according to require
2 min read
Draw Multiple Time Series in Same Plot in R Time Series in R programming language is used to see how an object behaves over a period of time. In R, it can be easily done by the ts() function with some parameters. Time series takes the data vector and each data is connected with timestamp value as given by the user. Method 1: Using Basic R me
2 min read
How to Plot Multiple Lines on an Excel Graph? Excel is a powerful data visualization and data management tool that can be used to store, analyze, and create reports on large data. It can be used to plot different kinds of graphs like line graphs, bar graphs, etc. \ The line graph is also known as a line plot or a line chart. In this graph, line
3 min read
Annotate Multiple Lines of Text to ggplot2 Plot in R In this article, we will see how to annotate Multiple Lines of text to ggplot2 Plot in R programming language. Let us first create a regular plot so that the difference is apparent, Example: R # Load Package library("ggplot2") # Create a DataFrame DF <- data.frame(X = runif(100, min=0, max=100),
2 min read