【问题标题】:Any way to make plot points in scatterplot more transparent in R?有什么方法可以使散点图中的绘图点在 R 中更加透明?
【发布时间】:2012-10-11 07:59:51
【问题描述】:

我有一个 3 列矩阵;绘图由基于第 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) 

【问题讨论】:

    标签: r plot ggplot2 alpha


    【解决方案1】:

    否则,您在包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%.
    

    【讨论】:

    • 我会说“为了 50% 的可见性”。例如,最好不要使用 50%,以便用户立即知道参数 alpha 的含义。
    • @DiegoFMedina 这其实不错,谢谢,我会尽快修改。
    • 请注意,库 scales 不是标准包,您可能需要预先安装它。
    【解决方案2】:

    创建颜色时,您可以使用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 设置添加到单组点。我怎样才能将此应用于多个组?
    • 例如:两种不同的颜色:points((1:10)+0.05, col=rgb(c(0, 0), c(1, 0), c(0, 1), rep(0.5, 2)), pch=16)(您可以对 6 种不同的颜色使用相同的方法)
    【解决方案3】:

    透明度也可以在颜色参数中编码。它只是另外两个十六进制数字,编码介于 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)
    

    【讨论】:

    • 我认为 base R 中的 ?adjustcolorgrDevices 包)非常类似,尽管它可能不像你的那样完全矢量化。
    • 嗨,萨莎!很抱歉“劫持”了答案:-)。我有一个简单的问题:semPlot 和/或qgraph [顺便说一句,优秀的包!] 有没有办法将标签(即载荷)放置在远离曲线中心的位置?不是沿着曲线,而是在“垂直”的意义上远离。我宁愿不诉诸低级 LaTeX 路线。谢谢!
    • 谢谢!不是现在,您只能使用 edge.label.position 参数来控制在边缘绘制标签的位置。我会努力实现的!
    【解决方案4】:

    如果您使用的是十六进制代码,您可以在代码末尾再添加两位数字来表示 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")
    

    【讨论】:

      【解决方案5】:

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

      例如

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

      【讨论】:

        猜你喜欢
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 2014-09-03
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多