How to plot a subset of a dataframe using ggplot2 in R ?

16 May, 2021
In this article, we will discuss how to plot a subset of a dataframe using ggplot2 in the R programming language.
Dataframe in use:
To get a complete picture, let us first draw a complete dataframe.
Example:
R
# Load ggplot2 packagelibrary(ggplot2)  # Create Data For plottingAge <- c("17", "18", "17", "19", "18", "19",         "17", "19", "18")  EnrollNo <- c("05", "10", "15", "20", "25",              "30", "35", "40", "45")  Score <- c("70", "80", "79", "75", "85", "96",           "90", "71", "83")  # Create a DataFrame from Datadata <- data.frame(Age, EnrollNo, Score)  # Create a simple Scatter Plotggplot(data, aes(Score, EnrollNo)) +   geom_point(color = "green", size = 3)
Output:
Simple Scatter Plot
Now, we will see all methods to create an R Plot using subset of DataFrame one by one.
Here, we use subset() function for plotting only subset of DataFrame inside ggplot() function inplace of data DataFrame. All other things are same.
Syntax: subset(obj, ...)
Parameters:
It takes data object to be subsetted as it’s first parameter.
subset() function can have many other parameters. only obj is necessary to take. here we only use logical expression indicating rows as a second argument.
Return : subset() function returns subset of DataFrame.
Example:
R
# Load ggplot2 packagelibrary(ggplot2)  # Create Data For plottingAge <- c("17", "18", "17", "19", "18", "19",         "17", "19", "18")  EnrollNo <- c("05", "10", "15", "20", "25",               "30", "35", "40", "45")  Score <- c("70", "80", "79", "75", "85", "96",           "90", "71", "83")    # Create a DataFrame from Datadata <- data.frame(Age, EnrollNo, Score)  # Generate R Scatter Plot only where Age # variable has value "18"ggplot(subset(data, Age %in% "18"), aes(Score, EnrollNo)) +   geom_point(color = "green", size = 3)
Output:
Scatter Plot using only subset of DataFrame by sunset() function 
In this method we are not using subset() function, but we write logical expression for retrieve subset of DataFrame in Square Brackets and assign it to subset variable.
Example:
R
# Load ggplot2 packagelibrary(ggplot2)  # Create Data For plottingAge <- c("17", "18", "17", "19", "18", "19",          "17", "19", "18")  EnrollNo <- c("05", "10", "15", "20", "25",              "30", "35", "40", "45")  Score <- c("70", "80", "79", "75", "85", "96",           "90", "71", "83")  # Create a DataFrame from Datadata <- data.frame(Age, EnrollNo, Score)  # Create a Variable 'data_subset' # which has the values equal to # "19" values of Age object.data_subset <- data[data$Age %in% "19", ]  # Generate R Scatter Plot only where# Age variable has value "19"ggplot(data_subset, aes(Score, EnrollNo)) +   geom_point(color = "green", size = 3)
Output:
Scatter Plot using only subset of DataFrame by Square Brackets 
In this method, We use square brackets to write expression for subset of DataFrame, but we use it inside ggplot() function in place of dataframe object. 
Example
R
# Load ggplot2 packagelibrary(ggplot2)  # Create Data For plottingAge <- c("17", "18", "17", "19", "18", "19",         "17", "19", "18")  EnrollNo <- c("05", "10", "15", "20", "25",               "30", "35", "40", "45")  Score <- c("70", "80", "79", "75", "85", "96",            "90", "71", "83")  # Create a DataFrame from Datadata <- data.frame(Age, EnrollNo, Score)  # Generate R Scatter Plot only where# Age variable has value "17"ggplot(data[data$Age %in% "17", ], aes(Score, EnrollNo)) +   geom_point(color = "green", size = 3)
Output: 
Picked
R-DataFrame
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming