Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio.

Charts and graphs are plotted in the R studio to represent those data pictorially.

Types of R – Charts:

  1. Bar Chart
  2. Pie Chart
  3. Line Chart
  4. Donut Chart
  5. Area Chart
  6. Dot Chart
  7. Pareto Chart
  8. X Bar & R Chart

  and so on…

We will discuss some charts in this article in detail.

1. Line Chart:

  • A line chart is a type of chart that can be used to display all the information in the form of a series of data points.
  • Line charts are generally used when we have to identify the trends in our dataset.
  • The data points are ordered in one of their coordinate value (mostly it is x - coordinate).

Syntax:

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

Where, v = vector

type = type “p” values to draw points only, type “l” values to draw lines only and type “o” is used to draw both the points and lines.

col = it gives colors to both the lines and points

 xlab = label for x-axis

ylab = label for y-axis

main = main stands for the title of the line chart

For example,

CODE:

x <- c(1:5)
y <- x


par(pch=19, col="red")


par(mfrow=c(2,6))


op = c("l")


for(i in 1:length(op))
{
  heading = paste("type",op[i])
  plot(x,y, type="n", main=heading)
  lines(x, y, type=op[i])
}

OUTPUT:

VARIOUS TYPES OF CHARTS IN R PROGRAMMING

2. Donut Chart:

  • A donut or Doughnut charts are also known as ring charts. It is just an alternative to a pie chart which can be created in ggplot2.
  • Donut chart is a ring that is divided into sectors, and each sector represents a proportion of the whole.
  • As it is very similar to a pie chart. Thus, it suffers the same problem.
  • Donut charts & Pie charts both charts can be plotted using a very similar process in R programming.

CODE:

# The first step is to install the following packages.
# install.packages("ggplot2")
# install.packages("dplyr")


library(ggplot2)
library(dplyr)


# We will Increase the value to make the hole bigger
# We will Decrease the value to make the hole smaller


hsize <- 4


df <- df %>% 
  mutate(x = hsize)


ggplot(df, aes(x = hsize, y = value, fill = group)) +
  geom_col() +
  coord_polar(theta = "y") +
  xlim(c(0.2, hsize + 0.5))

OUTPUT:

VARIOUS TYPES OF CHARTS IN R PROGRAMMING

3. Area Chart:

  • Area charts are really useful when we have to visualize one or more variables over some time.
  • An area chart is a kind of line plot which represents the distribution of quantitative type of data.
  • Even that area chart is plotted using the package ggplot2.

CODE:

#imports the ggplot2 library
library(ggplot2)


#creates the dataframe having the normal distribution values (rnorm)
x<-1:60
y<-cumsum(rnorm(60))
data1<-data.frame(x,y)


#plots the area chart
ggplot(data1, aes(x=x, y=y))+geom_area(fill='#142F86',alpha=2)

OUTPUT:

VARIOUS TYPES OF CHARTS IN R PROGRAMMING

4. Dot Chart:

  • A dot chart is used to create a dotted chart of the specific data.
  • A dot chart can be defined as a plot used to plot a Cleveland dot plot.
  • A dot chart is very similar to a scatter plot.
  • The only difference between a dot plot and a scatter plot is that it displays the index on the vertical axis and the corresponding value on the horizontal axis.

So, this helps us see each observation's values by following a horizontal line from the label.

Syntax:

Dotchart(x, labels = NULL, groups = NULL, gcolor = par(“fg”), color = par(“fg”))

Where,

x = matrix

labels: labels of vector for each and every data point

groups: It indicates how x variables are grouped. It is a grouping variable

gcolor: gcolor is a color that is used for group labels and values

color:  color is used for points and labels

CODE:

set.seed(1)


month <- month.name
expected sale<- c(15, 16, 20, 31, 11, 6,
                  17, 22, 32, 12, 19, 20)
sold <- c(8, 18, 12, 10, 41, 2,
          19, 26, 14, 16, 9, 13)
quarter <- c(rep(1, 3), rep(2, 3), rep(3, 3), rep(4, 3))


data <- data.frame(month, expected sale, sold, quarter)
data




dotchart(data$sold, labels = data$month, pch = 21, bg = "pink", pt.cex = 1.5)

OUTPUT:

VARIOUS TYPES OF CHARTS IN R PROGRAMMING

5. Pareto Chart:

  • Pareto Chart is a combination of a bar chart and a line chart that can be used for visualization.
  • A pareto chart or graph is a type of graph that displays the frequencies of different categories with the cumulated frequencies of the categories.
  • The right vertical axis is used for cumulative frequency, and the left vertical axis represents the frequency.
  • This graph uses the Pareto principle. Pareto’s principle states that 80% of effects are produced from 20% of causes of systems.

Syntax:

pareto.chart(x, ylab = “Frequency”, ylab2 = “Cumulative Percentage”, xlab, cumperc = seq(0, 100, by = 10), ylim, main, col = heat.colors(length(x)))

CODE:

library(qcc)


df <- data.frame(product=c('A', 'B', 'C', 'D', 'E', 'F'),
                 count=c(30, 67, 45, 88, 14, 12))
print(df)


pareto.chart(df$count)

OUTPUT:

VARIOUS TYPES OF CHARTS IN R PROGRAMMING

Related Topics

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

4 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

R Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.