首页 文章

R - 散点图的颜色键

提问于
浏览
0

我在R工作,正在创建一个3变量散点图,x和y对应于位置,颜色由z给出 .

但是,由于我的项目的细节,我无法使用现有的调色板,而是编写了一个直接将数据转换为rgb值的函数 .

我能够让图形看起来像我想要的方式(颜色是正确的),但我不知道如何创建合适的颜色键 .

假设我已经完成了我想做的所有处理,现在有一个数据结构,其中第1列是x值,第2列是y值,第3列是rgb值我希望是该点的颜色,第4列是用于生成给定点的颜色十六进制值的分数,我怎样才能最好地将其显示为带有颜色键的散点图?

我想要键覆盖整个颜色范围,从0到最高分 .

分数(第4列)不需要在图表中 - 它们仅用于分配颜色十六进制值,以及确定颜色键的范围 .

1 回答

  • 1

    以下是使用spatstat和plotrix如何使用调色板和贴图的简单示例:

    library("spatstat") ; library("plotrix")
    
    #your data:
    x <- 1:3
    # the colours included
    colors <- c("#FF0000" , "#00FF00" , "#0000FF")
    n.colors <- 100 # number of colours to interpolate over
    
    plot(1:3 , col = colors , pch = 16)
    
    # interpolate colours:
    palette <- colorRampPalette(colors, space = "rgb")(n.colors)
    color.map <- colourmap( palette , range=range(x) )
    color.range <- color.map( seq(min(x), max(x), length.out = n.colors) )
    
    #the labels of the legend
    col.labels <- round(seq(min(x),max(x),length=3) ,digits=1)
    color.legend( xl =2.5 , yb = 1, xr = 2.7, yt = 2 , # the coordinates
                  legend = col.labels , gradient="y", 
                  rect.col=color.range, align="rb")
    

    enter image description here

相关问题