How To Plot A Subset Of A Data Frame In R - - Statology
Maybe your like
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 1Example 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.
Related: How to Use with() and within() Functions in R
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Create Scatter Plots by Group in R How to Create a Scatterplot Matrix in R
Tag » How To Plot A Dataframe In R
-
How To Plot All The Columns Of A Dataframe In R ? - GeeksforGeeks
-
Data Frames And Plotting
-
How To Plot All The Columns Of A Data Frame In R - Stack Overflow
-
Plotting And Data Visualization In R | Introduction To R - ARCHIVED
-
Plot Method For Data Frames - R
-
Plot All Columns Of Data Frame In R (3 Examples) - Statistics Globe
-
Plot All Columns Of Data Frame In R (3 Examples) | Base R Vs. Ggplot2
-
Basic Scatterplot In Base R - The R Graph Gallery
-
Plot.dataframe: Plot Method For Data Frames
-
Data Visualization With Ggplot2 - Data Carpentry
-
4.1 Basic Plotting With Ggplot2 | Mastering Software Development In R
-
How Do I Create Plots In Pandas? — Pandas 1.5.0 Documentation
-
How To Create Scatterplot Using Data Frame Columns In R?
-
R Line Chart, Step By Step - Sharp Sight Labs