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.
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.
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.
The shapes in the existing answer are outdated. These are the current ones:
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))
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With