Suppose I have geographic data (long lat) for many people, and 2 properties for each person (size and sex), and I want to plot them placing the points with the long-lat data, and shaping the points with a two-colored square, the left half according to the size, the right one according to the sex. I use the following trick to manage to plots those 2-colored squares (inspired from here):
library(ggplot2)
library(Cairo)
dataframe = read.table(text =
"Lat Long Size Sex
47.875 6.787 small F
47.684 7.032 big M
47.644 6.942 small M
47.609 7.070 big F
47.460 7.197 big F
47.508 7.110 small F
47.442 7.006 big M
47.364 7.154 small F
47.348 7.455 big M
47.264 7.013 big F", header = TRUE)
colors <- c("big" = "firebrick3", "small" = "dodgerblue4", "M" = "gold", "F" = "forestgreen")
g <- ggplot(data = dataframe, mapping = aes(x = Long, y = Lat)) +
geom_point(aes (color=Size), shape="◧", size=30) +
geom_point(aes (color=Sex), shape="◨", size=30) +
geom_point(color="black", shape="◫", size=30) +
scale_color_manual(values=colors, "")
g
Suppose now that the points will HAVE TO overlap, for 2 reasons: they can't be to small because of readability of the colors, and they will be way too numerous (like 1000 or so)...
And suppose I have found a satisfying scale. This gives the following result.
The problem is with the way the points overlap. I sorted the data so that the lower points overlap on the higher ones. This is just a matter of choice. But it does not work well since the rights halves are plotted on top of the left ones, and the black rectangles on top of the whole rest. As one can see on the resulting image, a green half in the middle covers the red half below it, and all black rectangles are displayed on top.
My question: How do I plot the left half, the right half, and the black rectangles for the first point, and then the same again for the second one, etc., so that the overlapping is as I want, namely the upper points covered by the lower ones?