任何使散点图中的绘图点在R中更透明的方法?

我有一个3列matrix; 根据第1列和第2列值绘制点图,但是根据第2列(6个不同组)绘制颜色。 我可以成功地绘制所有的点,但是最后一个被分配了紫色的绘图组(组6)掩盖了其他组的绘图。 有没有办法让情节点更透明?

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) 

否则,你可以在包scales中使用alpha函数,你可以直接input你的颜色vector(即使它们是你的例子中的因子):

 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.5), pch=16) # For an alpha of 0.5, ie a transparency of 50%. 

你的意思是这样吗?

 plot(1:10, col=rgb(1, 0, 0, 0.5), pch=16) points((1:10)+0.05, col=rgb(0, 0, 1, 0.5), pch=16) 

详情请参阅?rgb

透明度也可以在颜色参数中进行编码。 它只是两个hex数字,编码0(完全透明)和255(完全可见)之间的透明度。 我曾经写过这个函数来为一个颜色向量添加透明度,也许这是有用的吗?

 addTrans <- function(color,trans) { # This function adds transparancy to a color. # Define transparancy with an integer between 0 and 255 # 0 being fully transparant and 255 being fully visable # Works with either color and trans a vector of equal length, # or one of the two of length 1. if (length(color)!=length(trans)&!any(c(length(color),length(trans))==1)) stop("Vector lengths not correct") if (length(color)==1 & length(trans)>1) color <- rep(color,length(trans)) if (length(trans)==1 & length(color)>1) trans <- rep(trans,length(color)) num2hex <- function(x) { hex <- unlist(strsplit("0123456789ABCDEF",split="")) return(paste(hex[(xx%%16)/16+1],hex[x%%16+1],sep="")) } rgb <- rbind(col2rgb(color),trans) res <- paste("#",apply(apply(rgb,2,num2hex),2,paste,collapse=""),sep="") return(res) } 

一些例子:

 cols <- sample(c("red","green","pink"),100,TRUE) # Fully visable: plot(rnorm(100),rnorm(100),col=cols,pch=16,cex=4) # Somewhat transparant: plot(rnorm(100),rnorm(100),col=addTrans(cols,200),pch=16,cex=4) # Very transparant: plot(rnorm(100),rnorm(100),col=addTrans(cols,100),pch=16,cex=4) 

如果您决定使用ggplot2 ,则可以使用alpha参数设置重叠点的alpha

例如

 library(ggplot2) ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 1/40) 

如果您使用的是hex代码,则可以在代码末尾添加两个数字来表示Alpha通道:

例如半透明红色:

 plot(1:100, main="Example of Plot With Transparency") lines(1:100 + sin(1:100*2*pi/(20)), col='#FF000088', lwd=4) mtext("use `col='#FF000088'` for the lines() function") 

颜色与透明度的示例图