0% found this document useful (0 votes)
96 views8 pages

Types of Graphs in R Programming

R language is mostly used for statistics and data analytics to represent data graphically using charts and graphs. It includes standard graphics, lattice graphs, and ggplot2 packages for creating various types of charts and graphs. Common graph types in R include bar plots, pie charts, histograms, scatter plots, and box plots. Each graph type has its own function and syntax for generating the graph from data in R.

Uploaded by

Disha Khurana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views8 pages

Types of Graphs in R Programming

R language is mostly used for statistics and data analytics to represent data graphically using charts and graphs. It includes standard graphics, lattice graphs, and ggplot2 packages for creating various types of charts and graphs. Common graph types in R include bar plots, pie charts, histograms, scatter plots, and box plots. Each graph type has its own function and syntax for generating the graph from data in R.

Uploaded by

Disha Khurana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

R language 

is mostly used for statistics and data analytics purposes to


represent the data graphically in the software. To represent those data
graphically, charts and graphs are used in R. 

R includes at least three graphical systems, the standard graphics package, the lattice package for


Trellis graphs and the grammar-of-graphics ggplot2 package
R – graphs
There are hundreds of charts and graphs present in R. For example, bar plot,
box plot, mosaic plot, dot chart, coplot, histogram, pie chart, scatter graph, etc. 

Types of R – Charts

 Bar Plot or Bar Chart


 Pie Diagram or Pie Chart
 Histogram
 Scatter Plot
 Box Plot

Barplot Definition: A barplot (or barchart; bargraph) illustrates the association between


a numeric and a categorical variable. The barplot represents each category as a bar and
reflects the corresponding numeric value with the bar’s size.

The following R syntax shows how to draw a basic barplot in R:

barplot(data, xlab, ylab)


barplot(x)
values <- c(0.4, 0.75, 0.2, 0.6, 0.5) # Create values for barchart
barplot(values)

barplot(values,col = "#1b98e0")

where:
 data is the data vector to be represented on y-axis
 xlab is the label given to x-axis
 ylab is the label given to y-axis
# defining vector
x <- c(7, 15, 23, 12, 44, 56, 32)
 
# output to be present as PNG file
png(file = "[Link]")
 
# plotting vector
barplot(x, xlab = "GeeksforGeeks Audience",
        ylab = "Count", col = "white",
        [Link] = "darkgreen",
        [Link] = "darkgreen")
 
# saving the file
[Link]()

Pie Diagram or Pie Chart

Pie chart is a circular chart divided into different segments according to the ratio
of data provided. The total value of the pie is 100 and the segments tell the
fraction of the whole pie. It is another method to represent statistical data in
graphical form and pie() function is used to perform the same. 
Syntax: pie(x, labels, col, main, radius)
where, 
 x is data vector
 labels shows names given to slices
 col fills the color in the slices as given parameter
 main shows title name of the pie chart
 radius indicates radius of the pie chart. It can be between -1 to +1

Note: To know about more optional parameters in pie() function, use the below
command in the R console: 
help("pie")
Example: 
Assume, vector x indicates the number of articles present on the
GeeksforGeeks portal in categories names(x) 
x <- c(210, 450, 250, 100, 50, 90)
 
# defining labels for each value in x
names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")
 
# output to be present as PNG file
png(file = "[Link]")
 
# creating pie chart
pie(x, labels = names(x), col = "white",
main = "Articles on GeeksforGeeks", radius = -1,
[Link] = "darkgreen")
 
# saving the file
[Link]()

Pie chart in 3D can also be created in R by using following syntax but


requires plotrix library. 
Syntax: pie3D(x, labels, radius, main)
Note: To know about more optional parameters in pie3D() function, use below
command in R console: 
help("pie3D")
library(plotrix)
 
# defining vector x with number of articles
x <- c(210, 450, 250, 100, 50, 90)
 
# defining labels for each value in x
names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")
 
# output to be present as PNG file
png(file = "[Link]")
 
# creating pie chart
pie3D(x, labels = names(x), col = "white",
main = "Articles on GeeksforGeeks",
labelcol = "darkgreen", [Link] = "darkgreen")
 
# saving the file
[Link]()
Histogram

Histogram is a graphical representation used to create a graph with bars


representing the frequency of grouped data in vector. Histogram is same as bar
chart but only difference between them is histogram represents frequency of
grouped data rather than data itself.
Syntax: hist(x, col, border, main, xlab, ylab)
where:
 x is data vector
 col specifies the color of the bars to be filled
 border specifies the color of border of bars
 main specifies the title name of histogram
 xlab specifies the x-axis label
 ylab specifies the y-axis label

 x <- c(21, 23, 56, 90, 20, 7, 94, 12,


     57, 76, 69, 45, 34, 32, 49, 55, 57)
  
 # output to be present as PNG file
 png(file = "[Link]")
  
 # hist(x, main = "Histogram of Vector x",
         xlab = "Values",
         [Link] = "darkgreen",
         [Link] = "darkgreen")
  
 # saving the file
 [Link]()
Scatter Plot

A Scatter plot is another type of graphical representation used to plot the points
to show relationship between two data vectors. One of the data vectors is
represented on x-axis and another on y-axis. 
Syntax: plot(x, y, type, xlab, ylab, main)
Where, 
 x is the data vector represented on x-axis
 y is the data vector represented on y-axis
 type specifies the type of plot to be drawn. For example, “l” for lines, “p” for
points, “s” for stair steps, etc.
 xlab specifies the label for x-axis
 ylab specifies the label for y-axis
 main specifies the title name of the graph

Note: To know about more optional parameters in plot() function, use the below
command in R console: 
help("plot")
orange <- Orange[, c('age', 'circumference')]
 
# output to be present as PNG file
png(file = "[Link]")
 
# plotting
plot(x = orange$age, y = orange$circumference, xlab = "Age",
ylab = "Circumference", main = "Age VS Circumference",
[Link] = "darkgreen", [Link] = "darkgreen",
[Link] = "darkgreen")
 
# saving the file
[Link]()
If a scatter plot has to be drawn to show the relation between 2 or more vectors
or to plot the scatter plot matrix between the vectors, then pairs() function is
used to satisfy the criteria. 
Syntax: pairs(~formula, data)
where,
 ~formula is the mathematical formula such as ~a+b+c
 data is the dataset form where data is taken in formula

Note: To know about more optional parameters in pairs() function, use the
below command in R console: 
help("pairs")
Example : 
png(file = "[Link]")
 
# plotting scatterplot matrix
# using dataset Orange
pairs(~age + circumference, data = Orange,
[Link] = "darkgreen")
 
# saving the file
[Link]()
Box Plot

Box plot shows how the data is distributed in the data vector. It represents five
values in the graph i.e., minimum, first quartile, second quartile(median), third
quartile, the maximum value of the data vector.
Syntax: boxplot(x, xlab, ylab, notch)
where, 
 x specifies the data vector
 xlab specifies the label for x-axis
 ylab specifies the label for y-axis
 notch, if TRUE then creates notch on both the sides of the box

Note: To know about more optional parameters in boxplot() function, use the


below command in R console: 
help("boxplot")
Example: 
x <- c(42, 21, 22, 24, 25, 30, 29, 22,
    23, 23, 24, 28, 32, 45, 39, 40)
 
# output to be present as PNG file
png(file = "[Link]")
 
# plotting
boxplot(x, xlab = "Box Plot", ylab = "Age",
[Link] = "darkgreen", [Link] = "darkgreen")
 
# saving the file
[Link]()

Common questions

Powered by AI

R provides three main graphical systems: the standard graphics package, the lattice package for Trellis graphs, and the ggplot2 package based on the grammar of graphics. The standard graphics package is the base R system for plotting, which is quite flexible but not as structured in layering as ggplot2. Lattice is specifically designed for creating conditioned plots, useful in multivariate data visualization. ggplot2, on the other hand, allows creating complex multilayered plots using a more systematic approach similar to writing a grammar, helping in building complex visualizations through a more intuitive syntax .

A scatter plot helps in understanding relationships by displaying points representing the values of two data vectors on the x and y axes. The spatial distribution of these points can reveal correlations, trends, and potential outliers between the two datasets. In R, the syntax for creating a scatter plot is `plot(x, y, type, xlab, ylab, main)`, where `x` and `y` are the data vectors plotted on the respective axes, and additional parameters specify the type of plot and axis labels .

Choosing ggplot2 over the standard graphics package may be favored for its systematic, layered approach to visualization, allowing more complex, customizable, and aesthetically pleasing plots through a grammar of graphics. This can be particularly advantageous for large, detailed datasets requiring multi-layered visual representation. However, the trade-offs include a steeper learning curve and potentially more verbose syntax compared to the more straightforward commands of the standard graphics package, which can be sufficient for simpler plots .

The primary difference between a histogram and a bar chart is that a histogram represents the frequency of grouped numerical data, illustrating data distribution, whereas a bar chart represents categorical data values using bar lengths proportional to those values. This means histograms are useful for depicting the distribution of a continuous dataset, while bar charts are suitable for categorical comparisons. The different uses stem from these fundamental distinctions, with histograms focusing on intervals of data and bar charts focusing on specific categories .

The use of color in R graphs serves both aesthetic and functional purposes, enhancing visual appeal and aiding differentiation between data elements. However, interpretative challenges arise as inappropriate or excessive use of color can clutter the visualization, obscure data insights, and pose accessibility issues for colorblind viewers. Proper use of contrasting colors helps in maintaining clarity, while a consistent color scheme improves comprehension. Analysts must balance these factors to effectively communicate data stories .

Bar plots are significant as they visually illustrate the association between numeric and categorical variables by representing each category as a bar whose size reflects the corresponding numeric value. In R, the basic syntax to create a bar plot is `barplot(data, xlab, ylab)`, where `data` is the data vector on the y-axis, `xlab` is the label for the x-axis, and `ylab` is the label for the y-axis. This allows users to easily compare different categories by observing the lengths of the bars .

A box plot represents five key statistical values: the minimum, first quartile, median (second quartile), third quartile, and the maximum. Understanding these values is beneficial as they provide a succinct summary of the data's distribution, highlight the central tendency, and reveal potential outliers. This helps analysts quickly assess data variability, identify skewness, and determine whether data may contain anomalies that require further investigation .

To create a 3D pie chart in R, the plotrix library is required. The syntax used is `pie3D(x, labels, radius, main)`, where `x` is the data vector, `labels` represents the names given to slices, `radius` sets the pie's radius, and `main` provides the chart title. Using 3D pie charts could offer better spatial understanding for viewers compared to a standard 2D pie chart by visually emphasizing differences in segment sizes. However, 3D display can also distort perception of the size of slices, potentially misleading viewers if not interpreted correctly .

The pie() function in R is utilized to emphasize specific data portions by slicing a circle into segments that represent data fractions relative to a whole, helping viewers intuitively grasp proportions. Optional parameters such as `labels`, `col`, and `main` enhance visualization by allowing customized names for segments, color assignments for color-coding, and titles for context. This customization aids in making pie charts more informative and visually engaging, adapting them to suit varied presentation needs .

The pairs() function in R is used to create a scatter plot matrix which visualizes pairwise relationships between multiple vectors in a dataset. The syntax `pairs(~formula, data)` enables users to define formulas for multiple variables, displaying scatter plots for every pair of variables. This is significant in multivariate data analysis as it allows simultaneous observation of potential correlations or patterns among all variables in a dataset, facilitating deeper insights compared to plotting single scatter plots individually .

You might also like