Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)  
like image 448
Steve Avatar asked Oct 21 '12 06:10

Steve


People also ask

How do you make a point transparent in a scatter plot?

Utilize the alpha argument in our scatter method and pass in a numeric value between 0 and 1. A value of 0 will make the plots fully transparent and unable to view on a white background.

How do I set transparency in R?

The only way to specify alpha values at present are via the special colour name "transpar- ent", which produces a fully transparent white, or using the alpha graphical parameter in grid (see the examples below). The alpha graphical parameter is a number from 0 to 1 (opaque to transparent).

How do I change the color of my plot points in R?

Change R base plot point shapes To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name). For example, col = "blue" or col = "#4F6228" .


2 Answers

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%. 
like image 71
plannapus Avatar answered Oct 23 '22 08:10

plannapus


When creating the colors, you may use rgb and set its alpha argument:

plot(1:10, col = rgb(red = 1, green = 0, blue = 0, alpha = 0.5),      pch = 16, cex = 4) points((1:10) + 0.4, col = rgb(red = 0, green = 0, blue = 1, alpha = 0.5),        pch = 16, cex = 4) 

enter image description here

Please see ?rgb for details.

like image 26
sgibb Avatar answered Oct 23 '22 10:10

sgibb