【问题标题】:R - color scatterplot points by z value with legendR - z 值的颜色散点图和图例
【发布时间】:2013-11-24 03:04:34
【问题描述】:

我有一个散点图并希望通过分配给每个点的z 值来为这些点着色。然后我想在图的右侧获取图例,以使用平滑的色谱显示哪些颜色对应于 z 的值。

这里有一些您可以使用的x,y,z 值,因此这是一个可重现的示例。

x = runif(50)
y = runif(50)
z = runif(50) #determines color of the (x,y) point

我想最好的答案是对任何颜色函数都通用的答案,但我确实希望使用rainbow()

【问题讨论】:

  • 如何将其移至 Stack Overflow?
  • 我发现了一个类似的问题here! 我能够重现答案,看起来非常好。看看吧。
  • 我们已经为您迁移了它,@StanLe。您可以查看链接的线程。总帐。

标签: r


【解决方案1】:

翻译自this previous question:

library(ggplot2)
d = data.frame(x=runif(50),y=runif(50),z=runif(50))
ggplot(data = d, mapping = aes(x = x, y = y)) + geom_point(aes(colour = z), shape = 19)

【讨论】:

    【解决方案2】:

    如果你不想使用 ggplot2 我修改了其他人提供的解决方案,我不记得是谁了。

    scatter_fill <- function (x, y, z,xlim=c(min(x),max(x)),ylim=c(min(y),max(y)),zlim=c(min(z),max(z)),
                              nlevels = 20, plot.title, plot.axes, 
                              key.title, key.axes, asp = NA, xaxs = "i", 
                              yaxs = "i", las = 1, 
                              axes = TRUE, frame.plot = axes, ...) 
    {
      mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
      on.exit(par(par.orig))
      w <- (3 + mar.orig[2L]) * par("csi") * 2.54
      layout(matrix(c(2, 1), ncol = 2L), widths = c(1, lcm(w)))
      par(las = las)
      mar <- mar.orig
      mar[4L] <- mar[2L]
      mar[2L] <- 1
      par(mar = mar)
    
    # choose colors to interpolate
    levels <- seq(zlim[1],zlim[2],length.out = nlevels)
    col <- colorRampPalette(c("red","yellow","dark green"))(nlevels)  
    colz <- col[cut(z,nlevels)]  
    #   
    plot.new()
    plot.window(xlim = c(0, 1), ylim = range(levels), xaxs = "i", yaxs = "i")
    
    rect(0, levels[-length(levels)], 1, levels[-1L],col=col,border=col) 
    if (missing(key.axes)) {if (axes){axis(4)}}
           else key.axes
       box()
       if (!missing(key.title)) 
         key.title
       mar <- mar.orig
       mar[4L] <- 1
       par(mar = mar)
    
       # points
       plot(x,y,type = "n",xaxt='n',yaxt='n',xlab="",ylab="",xlim=xlim,ylim=ylim,bty="n")
       points(x,y,col = colz,xaxt='n',yaxt='n',xlab="",ylab="",bty="n",...)
    
       ## options to make mapping more customizable
    
            if (missing(plot.axes)) {
              if (axes) {
                title(main = "", xlab = "", ylab = "")
                Axis(x, side = 1)
                Axis(y, side = 2)
              }
            }
            else plot.axes
            if (frame.plot) 
              box()
            if (missing(plot.title)) 
              title(...)
            else plot.title
            invisible()
     }
    

    只需先运行该功能即可使用。很方便。

    # random vectors
    vx <- rnorm(40,0,1)
    vy <- rnorm(40,0,1)
    vz <- rnorm(40,10,10)
    
    scatter_fill(vx,vy,vz,nlevels=15,xlim=c(-1,1),ylim=c(-1,5),zlim=c(-10,10),main="TEST",pch=".",cex=8)
    

    如您所见,它继承了通常的绘图功能。

    【讨论】:

    • 你能修改这个以便我设置 zlim 吗?
    • 当我运行你的代码时,我得到: scatter_fill 中的错误(allX,allY,allZ,nlevels = 15,main = "TEST",pch = ".",:尝试应用非-函数
    • 您是否事先运行了 scatter_fill 函数?我必须先将它加载到工作区中,然后才能使用它。
    • 是的,我只是将所有代码都放在同一个文件中。正如您在上面列出的那样。我什至在最顶部添加了一个 rm(list=ls(all=T)) 。似乎是代码中的错误。这个错误甚至意味着什么?有什么想法吗?
    • 有效!除了...添加 xlim、ylim 和 zlim 参数怎么样?你能补充一下吗?
    【解决方案3】:

    在包latticeExtra 中使用levelplot 的另一种选择,具有三种不同的调色板。

    library(latticeExtra)
    levelplot(z ~ x + y, panel = panel.levelplot.points, col.regions = heat.colors(50))
    levelplot(z ~ x + y, panel = panel.levelplot.points,
      col.regions =colorRampPalette(brewer.pal(11,"RdYlGn"))(50))
    levelplot(z ~ x + y, panel = panel.levelplot.points, col.regions = rainbow(50))
    

    【讨论】:

      最近更新 更多