Search code examples
rplotpch

How to fill a single 'pch' point on the plot with two-colours?


If you take the code below, how can you change the filling of the third diamond, so it will be half-black, half-white? The solution should apply with any colors.

data <- c(1,2,3)
plot(data, pch=c(23,18,23), cex=c(2.5,3,2.5))

Solution

  • The pch characters are actual font symbols, so you will be limited to what is available in your fonts. Another alternative is to use the primitive plotting commands to make your own "symbols". This is very flexible, although there can be issues with resizing, etc., when mixing the two methods. This method is implemented in the nice my.symbols() example:

    require(TeachingDemos)
    
    bwDiamond <- function() {
        plot.new()
        polygon(c(0, 0.5, 1, 0.5, 0), c(0.5, 0, 0.5, 1, 0.5), lty=1)
        polygon(c(0.25, 0.5, 1, 0.75, 0.25), c(0.25, 0, 0.5, 0.75, 0.25), col=1)
    }
    
    data <- c(1,2,3)
    
    dev.new(width=4, height=4)
    plot(data, type='n')
    points(data[1:2], pch=c(23,18), cex=c(2.5,3))
    my.symbols(data[3], data[3], symb=bwDiamond, symb.plots=TRUE, inches=0.22)
    

    enter image description here

    See also this Q/A: Point symbols in R