R - 散点图的颜色键

时间:2016-01-27 17:58:17

标签: r plot colors legend scatter-plot

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

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

我能够让图表看起来像我想要的样子(颜色是正确的),但我不知道如何创建合适的颜色键。

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

我希望按键覆盖整个颜色范围,从0到最高分。

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

1 个答案:

答案 0 :(得分: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

相关问题