Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

pch

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))
like image 575
Geek On Acid Avatar asked Nov 19 '11 18:11

Geek On Acid


People also ask

How do you color a point in R?

Change R base plot point shapes You can change this to pch = 19 (solid circle) or to pch = 21 (filled circle). To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name). For example, col = "blue" or col = "#4F6228" .

How do I change the color of the dots on a scatter plot in R?

The different color systems available in R have been described in detail here. To change scatter plot color according to the group, you have to specify the name of the data column containing the groups using the argument groupName . Use the argument groupColors , to specify colors by hexadecimal code or by name .

What is PCH in R plot?

The pch stands for plot character. The pch contains numeric values rangers from 0 to 25 or character symbols (“+”, “.”, “;”, etc.) specifying in symbols (or shapes). The pch is the standard argument to set the character plotted in several R functions.

How many PCH are in R?

The 25 different points symbols are commonly used in R for making beautiful graphs.


2 Answers

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

like image 177
John Colby Avatar answered Sep 24 '22 13:09

John Colby


@JohnColby : Ok, here is a way to draw a half-coloured circle point, using grid.semicircle from lodplot package. It's not such tidy solution as yours with polygons, but it does the job:

 require(lodplot)
 dev.new(width=4,height=4)
 plot(1:3,type="n")
 grid.semicircle(100,150,10,5)#left circle on plot below
 grid.semicircle(100,150,10,5,side=3,col=1)
 grid.semicircle(200,150,10,5,side=2)#right circle on plot below
 grid.semicircle(200,150,10,5,side=4,col=1)

Half-circles plot example

like image 20
Geek On Acid Avatar answered Sep 24 '22 13:09

Geek On Acid