Open In App

Qplot in R

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will look working of Qplot in R Programming using Qplot function. The basic plot() function from the R base package and the function Qplot are extremely similar. It can be used to quickly construct and combine several plot kinds. It is still less customizable than the function ggplot(). Let’s see the implementation of it.

What is Qplot in R?

The Qplot() is a function that is present in the ggplot2 package. The Qplot is an acronym for Quick Plot as the name suggests this was introduced to quickly plot any type of graph without much relying on and remembering the specified functions of each of them. The functionality of this Qplot is very similar to the plot() function which is present in the R Base Package. It’s mostly used to plot simple graphs than complex graphs because it uses a consistent calling technique for all graphs.

Syntax of  Qplot function in R

Syntax : qplot(data,x,y,facets,geom,main,xlab,ylab,asp)

where,

  • data: the data frame needs to be plotted
  • x,y: used to specify aesthetics into each layer of the graph
  • geom: used to specify the geometric figures to draw. [ If x and Y are specified then Scatterplot, If only X is specified then “Histogram” “boxplot” to plot box plot, “violin” to plot violin plot etc,.]
  • main: to append title to the plot
  • xlab,ylab: used to append labels to the x and y-axis to the plot.
  • shape – used to define the shape of objects based on categorical attribute
  • colour – used to color the data points by considering an categorical attribute
  • size – used to specify the size of data point(s)

Importing Iris Dataset of R Programming

Before moving ahead, let’s import the dataset and the ggplot2 package. In this article, we are going to use the default dataset which is present in R (iris).

R




# Install required packages (ggplot2)
install.packages("ggplot2")
   
# Load the package into current working environment
library(ggplot2)
   
# Load the default iris dataset in R
data(iris)


Scatter Plot using Qplot

Once we load the dataset, now we can Scatterplot using qplot. The qplot is a function that is used to plot the graph quickly it’s going to plot the scatter plot of the given dataset if the two discrete attributes were specified.

R




# Conversion of data set to data frame
df <- data.frame(iris)
 
# Scatterplot using qplot()
qplot(Sepal.Length,Sepal.Width,data=iris,
      main="ScatterPlot using qplot()",
      xlab="Length",ylab="Width")


Output:

What is Qplot in R

 

Customized Scatter Plot Shape and Color

The shape of the color of the data points in the scatter plot can also be modified by defining the color and shape attributes in qplot() function as follows

R




library(ggplot2)
# Conversion of data set to data frame
df <- data.frame(iris)
 
# Scatterplot using qplot()
qplot(Sepal.Length,Sepal.Width,data=iris,colour=Species,
      main="ScatterPlot using qplot()",shape=Species,
      xlab="Length",ylab="Width")


Output:

Scatter Plot using Qplot

 

Bar Plot using qplot()

Now we can see the BarPlot using qplot. If only one attribute was specified in the qplot() function then it’s going to plot the Bar Plot.

R




# Conversion of data set to data frame
df <- data.frame(iris)
 
# Barplot using qplot()
qplot(Sepal.Length,data=iris,
      main="Bar Plot using qplot()",
      xlab="Length")


Output:

Bar Plot using qplot()

 

Customization of Graphs using geom of qplot() function

Example 1:

We can Customize the plot as Combined Plot using qplot.

R




# Conversion of data set to data frame
df <- data.frame(iris)
 
# Customized Combined plot using qplot()
qplot(Sepal.Length,Sepal.Width,data=iris,
      geom=c("path","point","jitter"),color=Species,
      main="Customized Combined Plot using qplot()",
      xlab="Length",ylab="Width")


Output:

 Graphs using geom of qplot() function

 

Example 2:

Violin Plot using geom of qplot() function.

R




library(ggplot2)
# Conversion of data set to data frame
df <- data.frame(iris)
 
# Violin plot using qplot()
qplot(Species,Sepal.Width,data=iris,colour=Species,
      main="ViolinPlot using qplot()",geom="violin",
      xlab="Species",ylab="Width")


Output:

Violin Plot using geom of qplot() function.

 

Example 3:

Box Plot using geom of qplot() function.

R




library(ggplot2)
# Conversion of default data set to data frame
df <- data.frame(iris)
# Box plot using qplot()
qplot(mapping=aes(x=Species,y=Sepal.Length),data=iris,
      main="Box Plot using qplot()",geom="boxplot",
      xlab="Species",ylab="Sepal Length")


Output:

Box Plot using geom of qplot() function.

 



Like Article
Suggest improvement
Share your thoughts in the comments