Graphical Representations in R

Many graphical representations in R Language are available for qualitative and quantitative data types. This post will only discuss graphical representations in R such as histograms, bar plots, and box plots.

Creating Histogram in R

To visualize a single variable, the histogram can be drawn using the hist() function in R. The use of histograms is to judge the shape and distribution of data in a graphical way. Histograms are also used to check the normality of the variable.

Let us attach the data from iris dataset.

attach(iris)
head(iris)
hist(Petal.Width)

We can enhance the histogram by using some arguments/parameters related to the hist() function in R. For example,

hist(Petal.Width,
  xlab = "Petal Width",
  ylab = "Frequency",
  main = "Histogram of Petal Width from Iris Data set",
  breaks = 10,
  col = "dodgerblue",
  border = "orange")
Graphical Representations in R Language

If these arguments are not provided, R will attempt to intelligently guess them, especially the number of breaks. See the YouTube tutorial for graphical representations of the histogram.

Creating Barplots in R

The bar plots are the best choice for visual inspection of a categorical variable (or a numeric variable with a finite number of values), or a rank variable. Usually, one can use bar plots for comparison purposes. The barplot() function can be used for visual inspection of a categorical variable.

library(mtcars)
barplot( table(cyl) )
barplot(table(cyl),
  ylab = "Frequency",
  xlab = "Cylinders (4, 6, 8)",
  main = "Number of cylinders ",
  col = "green",
  border = "blue")

Creating Boxplots in R

One can use Boxplots to visualize the normality, skewness, and existence of outliers in the data based on five-number summary statistics.

boxplot(mpg)
boxplot(Petal.Width)
boxplot(Petal.Length)

However, one can compare a numerical variable for different values of a categorical/grouping variable. For example,

boxplot(mpg ~ cyl, data = mtcars)
Graphical Representations in R Boxplot

The reads the formula mpg ~ cyl as: “Plot the mpg variable against the cyl variable using the dataset mtcars. The symbol ~ used to specify a formula in R.

boxplot(mpg ~ cyl, data =mtcars,
  xlab = "Cylinders",
  ylab = "Miles per Gallon",
  pch = 20,
  cex = 2,
  col = "pink",
  border = "black")
Graphical-representation-in-r

See How to perform descriptive statistics

Visit: MCQs and Quiz site https://gmstat.com

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading