Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycling through point shapes when more than 6 factor levels

Tags:

r

ggplot2

When using the aesthetic mapping shape within geom_point, I get the following message when the number of factors present exceeds 6:

"The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes difficult to discriminate; you have 15. Consider specifying shapes manually. if you must have them."

I tend to agree with the principle of limiting the number of distinct shapes, however when using shape in combination with color this should not be a problem.

Is there an elegant way to get ggplot to cycle through shapes, i.e. setting symbol7 = symbol1 etc? Right now it simply omits the points with factor level > 6.

like image 393
mitchus Avatar asked May 29 '13 11:05

mitchus


People also ask

What is the maximum number of shapes that you are allowed to use in ggplot2 by default?

ggplot2 will only use six shapes at a time. By default, additional groups will go unplotted when you use this aesthetic. For each aesthetic you use, the aes() to associate the name of the aesthetic with a variable to display.


3 Answers

plot symbols

as you can see you have many possibilities for shapes. When you reach >6 you have to set the number manually, in this way:

    ggplot(data=dat1, aes(x=x, y=y,group=method,shape=method,color=method))+
    geom_point() +
    scale_shape_manual(values=seq(0,15))

In this way you will have no warnings and you will get the corresponding symbols on the graph

Update

As Luchonacho pointed out there are many new shapes available. Remember that if you want to set them using a loop do not use aes() as it would temporally keep in memory the last plotting reference (i.e. only the last looped input) and plot only that one.

enter image description here

like image 94
Garini Avatar answered Nov 11 '22 16:11

Garini


The shapes in the existing answer are outdated. These are the current ones: enter image description here

As you can see, they are all called by numbers. If you use a symbol (as in the other answer), an error occurs.

If you have not so many more than 6, then it is easy to choose them manually. For example, if you have 10 lines, one alternative is:

ggplot(mydata, aes(x,y, colour = z)) + 
    geom_line() + scale_shape_manual(values = c(4,8,15,16,17,18,21,22,3,42)) 
like image 35
luchonacho Avatar answered Nov 11 '22 16:11

luchonacho


As pointed out by the other answers you need to use scale_shape_manual.

To repeat the desired symbols you can simply use rep(x, times). For example if you want to repeat the filled out symbols 14 to 18 (see luchonacho answer for a list of symbols), you can use the following:

ggplot(data, aes(x,y, colour = z)) + geom_point()
    scale_shape_manual(values = rep(15:18, 5))

This will repeat the symbols 15 to 18 five times so it is enough for 20 different values of z.

like image 4
Steohan Avatar answered Nov 11 '22 15:11

Steohan