简体   繁体   English

有什么方法可以使散点图中的绘图点在R中更透明?

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

I have a 3 column matrix; 我有一个3列矩阵; plots are made by points based on column 1 and column 2 values, but colored based on column 2 (6 different groups). 绘图是根据第1列和第2列的值按点绘制的,但根据第2列(6个不同的组)进行着色。 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. 我可以成功绘制所有点,但是最后一个被分配为紫色的绘图组(第6组)掩盖了其他组的图形。 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) 

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): 否则,您将在包装scales中具有功能alpha ,在其中可以直接输入颜色矢量(即使它们是示例中的因素):

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%.

When creating the colors, you may use rgb and set its alpha argument: 创建颜色时,可以使用rgb并设置其alpha参数:

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)

在此处输入图片说明

Please see ?rgb for details. 有关详细信息,请参见?rgb

Transparency can be coded in the color argument as well. 透明度也可以在color参数中编码。 It is just two more hex numbers coding a transparency between 0 (fully transparent) and 255 (fully visible). 它只是另外两个十六进制数字,编码的透明度介于0(完全透明)和255(完全可见)之间。 I once wrote this function to add transparency to a color vector, maybe it is usefull here? 我曾经写过这个函数来增加颜色矢量的透明度,也许在这里有用吗?

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[(x-x%%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)
}

Some examples: 一些例子:

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)

If you decide to use ggplot2 , you can set transparency of overlapping points using the alpha argument. 如果决定使用ggplot2 ,则可以使用alpha参数设置重叠点的alpha

eg 例如

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

If you are using the hex codes, you can add two more digits at the end of the code to represent the alpha channel: 如果使用的是十六进制代码,则可以在代码末尾再添加两个数字以表示alpha通道:

Eg half-transparency red: 例如半透明红色:

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")

具有透明度的颜色示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM