rplotggplot2alpha

Any way to make plot points in scatterplot more transparent in R?


I have a 3 column matrix; plots are made by points based on column 1 and column 2 values, but colored based on column 2 (6 different groups). I can successfully plot all points, however, the last plot group (group 6) which was assigned the color purple, masks the plots of the other groups. Is there a way to make the plot points more transparent?

s <- read.table("/.../parse-output.txt", sep="\t") 
dim(s) 
[1] 67124     3
x <- s[,1] 
y <- s[,2]
z <- s[,3] 
cols <- cut(z, 6, labels = c("pink", "red", "yellow", "blue", "green", "purple"))
plot(x, y, main= "Fragment recruitment plot - FR-HIT", ylab = "Percent identity", xlab = "Base pair position", col = as.character(cols), pch=16) 

Solution

  • Otherwise, you have function alpha in package scales in which you can directly input your vector of colors (even if they are factors as in your example):

    library(scales)
    cols <- cut(z, 6, labels = c("pink", "red", "yellow", "blue", "green", "purple"))
    plot(x, y, main= "Fragment recruitment plot - FR-HIT", 
         ylab = "Percent identity", xlab = "Base pair position", 
         col = alpha(cols, 0.4), pch=16) 
    # For an alpha of 0.4, i. e. an opacity of 40%.