How can I plot a subset of a data frame in R? 2

How can I plot a subset of a data frame in R?

To plot a subset of a data frame in R, first use the subset() function to create a new data frame containing only the desired subset of data. Then, use the plot() function to create a visual representation of the data. This allows for the selection and visualization of a specific portion of the original data frame, making it easier to analyze and interpret the data.

Plot a Subset of a Data Frame in R


You can use the following methods to plot a subset of a data frame in R:

Method 1: Plot Subset of Data Frame Based on One Condition

#plot var1 vs. var2 where var3 is less than 15
with(df[df$var3 < 15,], plot(var1, var2))

Method 2: Plot Subset of Data Frame Based on Multiple Conditions

#plot var1 vs. var2 where var3 is less than 15 and var4 is greater than 3
with(df[(df$var3 < 15) & (df$var4 > 3),], plot(var1, var2))

The following examples show how to use each method in practice with the following data frame:

#create data frame
df <- data.frame(A=c(1, 3, 3, 4, 5, 7, 8),
                 B=c(3, 6, 9, 12, 15, 14, 10),
                 C=c(10, 12, 14, 14, 17, 19, 20),
                 D=c(5, 7, 4, 3, 3, 2, 1))

#view data frame
df

  A  B  C D
1 1  3 10 5
2 3  6 12 7
3 3  9 14 4
4 4 12 14 3
5 5 15 17 3
6 7 14 19 2
7 8 10 20 1

Example 1: Plot Subset of Data Frame Based on One Condition

The following code shows how to create a scatter plot of variable A vs. variable B where variable C is less than 15:

#plot A vs. B where C is less than 15
with(df[df$C < 15,], plot(A, B))

Notice that only the rows in the data frame where variable C is less than 15 are shown in the plot.

Example 2: Plot Subset of Data Frame Based on Multiple Conditions

The following code shows how to create a scatter plot of variable A vs. variable B where variable C is less than 15 and variable D is greater than 3:

#plot A vs. B where C is less than 15 and D is greater than 3
with(df[(df$C< 15) & (df$D> 3),], plot(A, B))

Notice that only the rows in the data frame where variable C is less than 15 and variable D is greater than 3 are shown in the plot.

Cite this article

stats writer (2024). How can I plot a subset of a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-a-subset-of-a-data-frame-in-r/

stats writer. "How can I plot a subset of a data frame in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-a-subset-of-a-data-frame-in-r/.

stats writer. "How can I plot a subset of a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-a-subset-of-a-data-frame-in-r/.

stats writer (2024) 'How can I plot a subset of a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-a-subset-of-a-data-frame-in-r/.

[1] stats writer, "How can I plot a subset of a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I plot a subset of a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top