在R中,有没有办法使散点图中的数据点更透明?

83

我有一个三列矩阵,根据第一列和第二列的值以点为基础进行绘制,但根据第二列(六个不同组)进行染色。我可以成功地绘制出所有的点,然而,最后一个绘图组(组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) 
5个回答

115
否则,您可以在包中使用函数scalesalpha,直接输入颜色向量(即使它们是因子,就像在您的示例中一样):
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%.

1
我会说“为了50%的可见度”。例如,最好不要使用50%,这样用户就能立刻知道参数alpha的含义。 - Diego F Medina
@DiegoFMedina 这实际上不是一个坏主意,谢谢,我会尽快修改。 - plannapus
请注意,库scales不是标准包,您可能需要事先安装它。 - normanius

68

创建颜色时,您可以使用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)

这里输入图片描述

详见?rgb


似乎我只能将RGB设置添加到单个点组中。如何将其应用于多个组? - Steve
例如:两种不同的颜色:points((1:10)+0.05, col=rgb(c(0, 0), c(1, 0), c(0, 1), rep(0.5, 2)), pch=16)(您可以使用相同的方法来获得6种不同的颜色) - sgibb

18

透明度也可以通过颜色参数进行编码。只需再添加两个十六进制数字,表示透明度从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[(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)
}

一些示例:

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)

16
我认为R的基础包grDevices中的?adjustcolor函数与此非常相似,尽管它可能没有你的函数完全向量化。 - Ben Bolker
嗨,Sacha!抱歉“劫持”了答案:-)。我有一个快速问题:在semPlot和/或qgraph [顺便说一句,这些都是很棒的包!]中是否有一种方法可以将标签(即载荷)从曲线中心移开?不沿着曲线,而是以“垂直”的方式远离。我宁愿不采用低级LaTeX路线。谢谢! - Aleksandr Blekh
谢谢!目前你只能使用edge.label.position参数来控制标签绘制在边的哪个位置。我会尝试实现它! - Sacha Epskamp

16

如果您正在使用十六进制代码,您可以在代码末尾添加两个数字来表示α通道:

例如,半透明红色:

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

带透明度的颜色示例图


13
如果你决定使用ggplot2,可以使用alpha参数设置重叠点的透明度。
例如:
library(ggplot2)
ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 1/40)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接