Statistical Computing and Graphics in R

# Graphical Representation in R

In the R language, there is much graphical representation of qualitative and quantitative data. We will only discuss the histogram, bar plot, and box plot in this post.

### Histogram

To visualize a single variable, the histogram can be drawn using the hist( ) function. 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 the data from iris dataset.

attach(iris)
hist(Petal.Width)

We can enhance the histogram by using some arguments/parameters related to the hist( ) function. 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")

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

### Barplots

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. See the example,

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

### Boxplots

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)

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")

See How to perform descriptive statistics

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

Graphical Representation in R
Scroll to top