# Using the scales package to change alpha in base R plots

**R – Statistical Odds & Ends**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When you are plotting many points on one plot, changing the transparency, or ** alpha**, of the points is often a good idea. For example, the plot below plots price vs. carat for each of 53,940 diamonds:

library(ggplot2) ggplot(diamonds, aes(x = carat, y = price)) + geom_point()

We get a mess/mass of points! We can see the general trend between price and carat, but we don’t know how many diamonds there really are in each part of the plot. By reducing the alpha of the points, we get a cleaner, more informative plot:

library(ggplot2) ggplot(diamonds, aes(x = carat, y = price)) + geom_point(alpha = 0.05)

We immediately learn 2 things that we couldn’t tell from the previous plot: (i) there are a lot more small diamonds (<1 carat) than big ones; and (ii) the carat sizes of diamonds seem to clump just above “nice” values like 1, 1.5 and 2.

Changing the alpha of points is really easy in ggplot2. It turns out that it’s not too difficult in base R plots as well: we just need the scales package. When drawing the points, define the “col” option as a function call to the alpha function in scales. The code snippet below gives an example of how to do this:

library(scales) with(diamonds, plot(x = carat, y = price, pch = 19, col = alpha("black", 0.05)))

The code above produces the plot below:

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Statistical Odds & Ends**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.