Open In App

Data Visualization in R

Last Updated : 13 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Data visualization is the practice of representing data through visual elements like graphs, charts, and maps. It helps in understanding large datasets more easily, making it possible to identify patterns and trends that support better decision-making. R is a language designed for statistical analysis and data visualization. It is commonly used for creating visualizations because it provides a range of packages, such as ggplot2 and plotly, which allow users to create customized and effective visual representations of data.

R stands out in data visualization because it offers flexibility and precise control over the visual output, making it suitable for both simple and complex data analysis. This article focuses on how R can be used for data visualization and explores some of its widely-used tools and techniques.

Types of Data Visualizations

Some of the various types of visualizations offered by R are:

1. Bar Plot

There are two types of bar plots: horizontal and vertical which represent data points as horizontal or vertical bars of certain lengths proportional to the value of the data item. They are generally used for continuous and categorical variable plotting. By setting the horiz parameter to true and false, we can get horizontal and vertical bar plots respectively. 

1.1 Horizontal Bar Plot

R
barplot(airquality$Ozone,
        main = 'Ozone Concenteration in air',
        xlab = 'ozone levels', horiz = TRUE)

Output:

1.2 Vertical Bar Plot 

R
barplot(airquality$Ozone, 
        main = 'Ozone Concenteration in air', 
        xlab = 'ozone levels', col ='blue', horiz = FALSE)

Output:

Bar plots are used for the following scenarios:

  • To perform a comparative study between the various data categories in the data set.
  • To analyze the change of a variable over time in months or years.

2. Histogram

A histogram is like a bar chart as it uses bars of varying height to represent data distribution. However, in a histogram values are grouped into consecutive intervals called bins. In a Histogram, continuous values are grouped and displayed in these bins whose size can be varied.

For a histogram, the parameter xlim can be used to specify the interval within which all values are to be displayed. Another parameter freq when set to TRUE denotes the frequency of the various values in the histogram and when set to FALSE, the probability densities are represented on the y-axis such that they are of the histogram adds up to one.

Example: 

R
data(airquality)

hist(airquality$Temp, main ="La Guardia Airport's Maximum Temperature(Daily)",
    xlab ="Temperature(Fahrenheit)",
    xlim = c(50, 125), col ="yellow",
    freq = TRUE)

Output:

Histograms are used in the following scenarios: 

  • To verify an equal and symmetric distribution of the data.
  • To identify deviations from expected values.

3. Box Plot

The statistical summary of the given data is presented graphically using a boxplot. A boxplot depicts information like the minimum and maximum data point, the median value, first and third quartile and interquartile range.

Example: 

R
data(airquality)

boxplot(airquality$Wind, main = "Average wind speed at La Guardia Airport",
        xlab = "Miles per hour", ylab = "Wind",
        col = "orange", border = "brown",
        horizontal = TRUE, notch = TRUE)

Output:

Multiple box plots can also be generated at once through the following code:

Example: 

R
boxplot(airquality[, 0:4], 
        main ='Box Plots for Air Quality Parameters')

Output:

Box Plots are used for: 

  • To give a comprehensive statistical description of the data through a visual cue.
  • To identify the outlier points that do not lie in the inter-quartile range of data.

4. Scatter Plot

A scatter plot is composed of many points on a Cartesian plane. Each point denotes the value taken by two parameters and helps us easily identify the relationship between them.

Example: 

R
data(airquality)

plot(airquality$Ozone, airquality$Month,
     main ="Scatterplot Example",
     xlab ="Ozone Concentration in parts per billion",
     ylab =" Month of observation ", pch = 19)

Output:

Scatter Plots are used in the following scenarios: 

  • To show whether an association exists between bivariate data.
  • To measure the strength and direction of such a relationship.

5. Heat Map

Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. heatmap() function is used to plot heatmap.

Example:

R
data <- matrix(rnorm(25, 0, 5), nrow = 5, ncol = 5)

colnames(data) <- paste0("col", 1:5)
rownames(data) <- paste0("row", 1:5)

heatmap(data) 

Output:

heatmap
Heat Map

6. Map visualization in R

Here we are using maps package to visualize and display geographical maps using an R programming language.

install.packages("maps")

Example:

R
install.packages("maps")
library(maps)
map(database = "world")

df <- data.frame(
  city = c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix"),
  lat = c(40.7128, 34.0522, 41.8781, 29.7604, 33.4484),
  lng = c(-74.0060, -118.2437, -87.6298, -95.3698, -112.0740)
)

points(x = df$lng, y = df$lat, col = "Red")

Output:

worldmap
Map visualization

7. 3D Graphs in R 

Here we will use preps() function, This function is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane.

R
cone <- function(x, y){
sqrt(x ^ 2 + y ^ 2)
}
  
x <- y <- seq(-1, 1, length = 30)
z <- outer(x, y, cone)
  
persp(x, y, z,
      main="Perspective Plot of a Cone",
      zlab = "Height",
      theta = 30, phi = 15,
      col = "orange", shade = 0.4
      )

Output:

Application Areas 

  • Presenting analytical conclusions of the data to the non-analysts departments of your company.
  • Health monitoring devices use data visualization to track any anomaly in blood pressure, cholesterol and others.
  • To discover repeating patterns and trends in consumer and marketing data.
  • Meteorologists use data visualization for assessing prevalent weather changes throughout the world.
  • Real-time maps and geo-positioning systems use visualization for traffic monitoring and estimating travel time.

Advantages of Data Visualization in R

R has the following advantages over other tools for data visualization: 

  • R offers a broad collection of visualization libraries along with extensive online guidance on their usage.
  • R also offers data visualization in the form of 3D models and multi panel charts.
  • Through R, we can easily customize our data visualization by changing axes, fonts, legends, annotations and labels.

Disadvantages of Data Visualization in R

R also has the following disadvantages: 

  • R is only preferred for data visualization when done on an individual standalone server.
  • Data visualization using R is slow for large amounts of data as compared to other counterparts.

Next Article

Similar Reads