How to plot a subset of a dataframe in R ?
In this article, we will learn multiple approaches to plotting a subset of a Dataframe in R Programming Language. Here we will be using, R language’s inbuilt “USArrests” dataset.
Method 1: Using subset() function
In this method, first a subset of the data is created base don some condition, and then it is plotted using plot function. Let us first create the subset of the data.
Syntax: plot(subset( data, condition, select))
Parameters:
data: dataframe
condition: indicates the logical expression on the basis of which subsetting has to be done
select: indicates columns to select
Example:
R
subset (USArrests, Assault > 100 & UrbanPop > 25, select = c ( Rape, Assault)) |
Output:
Rape Assault
Alabama 21.2 236
Alaska 44.5 263
.
.
.
Washington 26.2 145
Wyoming 15.6 161
Now let’s create the plot for the subset so obtained. The above command can be directly passed to the plot() function.
Example:
R
plot ( subset (USArrests,Assault > 100 & UrbanPop > 25, select = c (Rape, Assault))) |
Output:
Method 2: Using [ ] operator
Using the ‘[ ]’ operator, elements of vectors and observations from dataframes can be accessed and subsetted based on some condition.
Syntax: plot( df$col1[condition], df$col2[condition] )
Parameters:
df: dataframe
condition: indicates the logical expression on the basis of which subsetting has to be done
Again the data will be first subsetted out and then will be provided to the plot() function.
Example:
R
plot (USArrests$Rape[USArrests$Assault > 100] , USArrests$Assault[USArrests$Assault > 100] ) |
Output:
Method 3: Using attributes for rows and columns
In this we pass the row and column name to be plotted, and the condition based on which sub setting should be done to the plot function.
Syntax: plot( x ~ y, data=subset(df, condition ) )
Parameters:
x: data for x axis
y: data for y axis
data: subset of our dataframe which we want to plot.
df: dataframe object
condition: indicates the logical expression on the basis of which subsetting has to be done
Example:
R
plot (Murder ~ UrbanPop, data= subset (USArrests, Rape < 25)) |
Output: