# How to 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.