0
votes

I'm working in R and am creating a 3-variable scatter plot, with x and y corresponding to position, and color given by z.

However, due to the specifics of my project, I couldn't use an existing color palette, and instead wrote a function directly converting data into rgb values.

I'm able to get the graph to look the way I want (the colors are correct), but I don't know how to create a suitable color key.

Assuming I've already done all the processing I want to do, and now have a data structure where column 1 is the x value, column 2 is the y value, column 3 is the rgb value I want to be the color of the point, and column 4 is the score used to generate the color hex value of a given point, how would I best display this as a scatter plot with color key?

I want the key to cover the entire color range and go from 0 to the max score.

The scores (column 4) don't need to be in the graph - they are just used to assign the color hex values, and to determine the range of the color key.

1

1 Answers

1
votes

Here is a simple example of how to work with colour palettes and maps using spatstat and 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