# R: How to Use alpha() to Change Transparency in Plots

Often you may want to change the transparency of points in a plot in R.

There are two common ways to do so by using the alpha() function in R:

Method 1: Use alpha() to Change Transparency in Base R

```library(scales)

plot(df\$points, df\$assists, col=alpha(df\$team, .5))
```

This particular example uses the alpha() function from the scales package to specify the transparency that should be used in points on a scatterplot in base R.

Method 2: Use alpha() to Change Transparency in ggplot2

```library(ggplot2)

ggplot(df, aes(points, assists, col=team)) +
geom_point(alpha=0.5)```

This particular example uses the alpha() function that comes built-in with the ggplot2 package to specify the transparency that should be used in points on a ggplot2 scatterplot.

Note that you can supply a value between 0 and 1 for the alpha argument in each method. A value of 0 will cause the points to be completely transparent while a value of 1 will cause the points to be completely visible.

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

```#create data frame
df <- data.frame(team=as.factor(1:2)),
points=c(99, 68, 86, 88, 95, 74, 78, 93),
assists=c(22, 28, 45, 35, 34, 45, 28, 31))

#view data frame
df

team points assists
1    1     99      22
2    2     68      28
3    1     86      45
4    2     88      35
5    1     95      34
6    2     74      45
7    1     78      28
8    2     93      31
```

The data frame contains information about various basketball players, including their team name, total points scored, and total assists.

## Example 1: Use alpha() to Change Transparency in Base R

Suppose we use the plot() function to create a scatterplot of points vs. assists in R:

```#create basic scatterplot of points vs assists
plot(df\$points, df\$assists, col=df\$team, cex=3, pch=19)
```

This produces the following plot:

Note: The cex argument specifies the size of the points in the plot and the pch argument specifies the symbol to use. A value of 19 represents a filled-in circle.

Since we didn’t use the alpha() function at all, the points in the plot are fully visible.

However, we could use the alpha() function from the scales package with a value of 0.5 to give the points half transparency:

```library(scales)

#create scatterplot with transparency value of 0.5
plot(df\$points, df\$assists, col=alpha(df\$team, .5), cex=3, pch=19)
```

Notice that the points in the plot now have half transparency since we used a value of 0.5 for the alpha argument in the plot() function.

## Example 2: Use alpha() to Change Transparency in ggplot2

Suppose we use the following syntax to create a scatterplot of points vs. assists in ggplot2:

```library(ggplot2)

#create scatterplot of points vs assists
ggplot(df, aes(points, assists, col=team)) +
geom_point(size=5)
```

This produces the following plot:

Since we didn’t use the alpha() function at all, the points in the plot are fully visible.

However, we could use the alpha() function with a value of 0.5 to give the points half transparency:

```library(ggplot2)

#create scatterplot with transparency of 0.5
ggplot(df, aes(points, assists, col=team)) +
geom_point(size=5, alpha=0.5)
```

Notice that the points in the plot now have half transparency since we used a value of 0.5 for the alpha argument in the geom_point() function.