0
votes

I attempting a scatter plot with many points (> 150). The goal is to distinguish points at certain areas of the graph. What I'm essentially looking for is a way to have 2 color scales for the x and y axes (1 for each). Essentially, I'm looking for something like this:

enter image description here

Each unique point should be a mix of the colours of the respective scales. What I have tried so far is a scatter plot using ggplot. I've tried setting the colour attribute, but that assigns its own coordinates. It also doesn't work with a limitation I have in that I have to create separate plots of the scatter plot (in short, zoomed in plots of the top-left, top-right, bottom-left, bottom-right). If I set the xlim and ylim to my own liking for the additional plots, all I get is a crop which results in some cutouts of other points and their texts on the edges of the plot. I can't simply create a separate plot as I need the points to be the same colour on my overall plot and the more specific plots (singular colours).

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0)
ggplotXY
dev.off()

Current overall plot:

enter image description here

Current plot of zoomed in bottom-left:

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0) +
coord_cartesian(xlim=c(0,100), ylim=c(0, 2.5))
ggplotXY
dev.off()

enter image description here

As you can see, some of the points are clipped and aren't ommitted. In order to leave out the non applicable points, I'll have to create a new data frame with the actual points within the limits, but doing so would alter the colours of the points when I create a new plot. I was thinking about including my own colours for each point as part of my data frame that I'm reading in, but adding and subtracting hex colour codes is not very nice. I tried and got something along these lines:

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXYColor <- ggplot(scatterPlotData, aes(x=x, y=y, label=labels)) +
geom_point(colour=scatterPlotData$scatterPointColour)
ggplotXYColor
dev.off()

enter image description here

In case you are wondering, the scatterPlotData$scatterPointColour is as follows:

  [1] "#2276c6" "#224dd0" "#201893" "#22459f" "#21580f" "#219998" "#201893"
  [8] "#216871" "#22459f" "#201893" "#2276c6" "#22459f" "#22353d" "#201893"
 [15] "#225602" "#21cabe" "#2178d3" "#21eb83" "#21eb83" "#201893" "#201893"
 [22] "#22978b" "#2276c6" "#301054" "#201893" "#301054" "#225e33" "#228f59"
 [29] "#226664" "#220c47" "#21eb83" "#228f59" "#227ef7" "#227ef7" "#226e95"
 [36] "#21c28d" "#22459f" "#228f59" "#223d6e" "#221caa" "#22459f" "#226e95"
 [43] "#225602" "#221caa" "#21d2f0" "#222d0c" "#22459f" "#201893" "#2020c4"
 [50] "#210623" "#21a1c9" "#201893" "#228f59" "#201893" "#201893" "#221caa"
 [57] "#220c47" "#201893" "#22a7ed" "#101893" "#22c080" "#201893" "#2276c6"
 [64] "#201893" "#201893" "#21d2f0" "#222d0c" "#21c28d" "#225602" "#226664"
 [71] "#226e95" "#201893" "#201893" "#21b22b" "#2020c4" "#21cabe" "#21f3b4"
 [78] "#22d0e2" "#201893" "#21c28d" "#21fbe5" "#220c47" "#225602" "#230209"
 [85] "#226664" "#210e55" "#211eb7" "#2170a2" "#201893" "#221caa" "#220c47"
 [92] "#21f3b4" "#21fbe5" "#201893" "#201893" "#201893" "#224dd0" "#247add"
 [99] "#201893" "#23fffc" "#25db1d" "#24188f" "#245a18" "#2449b6" "#24a3d3"
[106] "#201893" "#2451e7" "#24624a" "#24830e" "#2020c4" "#201893" "#201893"
[113] "#25b228" "#25eb80" "#23ced5" "#244185" "#24ed8d" "#243123" "#2449b6"
[120] "#201893" "#273b5e" "#201893" "#264dcd" "#2420c1" "#2578d0" "#264dcd"
[127] "#251eb3" "#22c8b1" "#22c080" "#22f1a7" "#249370" "#251eb3" "#2428f2"
[134] "#2428f2" "#249ba1" "#201893" "#2020c4" "#201893" "#244185" "#2472ac"
[141] "#2449b6" "#247add" "#201893" "#244185" "#243123" "#249370" "#24b435"
[148] "#2020c4" "#248b3f" "#2020c4"

I converted the hex colours to decimal and then added specific decimal colours together and then converted it back to hex. Theoretically, it should be a nice white to yellow on the x-axis and white to blue on the y-axis. As the points increase in x and y, the colours should become more green. As you can see, it's not as simple as that. I haven't come across any libraries that does the 2 axes colours.

To sum up, I need to be able to have the 2 axes colours to give unique colours to the points and a way to create additional plots that will have the exact some colours just on a more zoomed in canvas.

If anyone can help, it would be greatly appreciated.

1
I don't understand why you want to have such a plot. After all, you represent the same information twice (by color and by location). - ziggystar
The portion of the graph that is of greatest interest is at a certain location. By isolating the colours/region, it's easy to take a look at it. - intl

1 Answers

1
votes

Here you have a first approach using base graphics for your first problem (mixing two color gradients).

## use white->yellow for the x-axis and white->blue for the y-axis
chooseColors <- function(x, y) {
  x <- 1-x/max(x)
  y <- 1-y/max(y)
  return(rgb(green=y, red=y, blue=x))
}


## example values for the whole range
values <- expand.grid(1:100, 1:100)

## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)

enter image description here

A more realistic toy example:

set.seed(1)
n <- 50
values <- cbind(sample(1:15, size=n, replace=TRUE), sample(1:15, size=n, replace=TRUE))

## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)

enter image description here