Open In App

Change size of outlier labels on boxplot in R

Improve
Improve
Like Article
Like
Save
Share
Report

The boxplots in R Programming Language are used to label the data and take an assumption about how well distributed it is. The boxplot can be constructed using various data visualization packages in R, like the ggplot2 and the car packages. Outlier refers to the data points located outside the boundaries of the data. These are the data points that generally fall outside the proper boundaries of the other data points. It is possible to increase the size of the outliers.

Method 1: Using ggplot2 package

The ggplot2 package is used for data visualization and depicting the plots. This package can be downloaded and installed into the working space using the following command :

install.packages("ggplot2")

The ggplot method in this package is used to construct various kinds of plots, like scatter plots, box plots, etc. The plots take as input the data frame to be used and also supply aesthetic mappings using the x and y coordinates. Other arguments can be added by using the color specified by the grouping column.

The geom_boxplot() component can be added to the ggplot object in order to create a boxplot from the data specified. It takes as an argument the outlier.size in order to provide a size to the outlying points.

Syntax: geom_boxplot (outlier.size = )

Arguments :

outlier.size – The size of the outlying points of the box plot

R




# installing the required libraries
library("ggvis")
  
# creating a data frame
data_frame <- data.frame(col1 = c("a","b","a",
                                  "a","b","a"),
                         col2 = c(1,2,3,4,5,12))
print("Data Frame")
print(data_frame)
  
# plotting with notch
ggplot(data_frame, aes(x = col1, y = col2)) +
  geom_boxplot(outlier.size=20)


Output

[1] "Data Frame"
> print(data_frame)
  col1 col2
1    a    1
2    b    2
3    a    3
4    a    4
5    b    5
6    a   12

 

Method 2: Using car package

The  Companion to Applied Regression (car) package in R is used to apply the regression techniques to the data elements contained in R objects. The package can be downloaded and installed into the R working space using the following command :

install.packages("car")

The Boxplot method in R can be used to create boxplots with the feature of point identification.

Syntax: Boxplot( vec, data, labels, outcex )

Arguments :

  • vec – The variable for which the boxplot is constructed
  • data – The data table
  • labels – The label to provide to the plotted data
  • outcex – The size of the outlier

The following code snippet uses the col1 of the data frame to plot boxplot of the data points. The labels is assigned as the column header of the data frame. The outcex parameter can be used to assign the size to the outlier.

R




# installing the required libraries
library("car")
# creating a data frame
data_frame <- data.frame(col1 = c("a","b","a",
                                  "a","b","a"),
                         col2 = c(1,2,3,4,5,12))
print("Data Frame")
print(data_frame)
  
# plotting with notch
Boxplot(data_frame$col2, data=data_frame, 
        labels=row.names(data_frame),
        outcex=3)


Output

[1] "Data Frame"
col1 col2

1    a    1
2    b    2
3    a    3
4    a    4
5    b    5
6    a   12

[1] 6

 



Last Updated : 23 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads