简体   繁体   中英

Cycling through point shapes when more than 6 factor levels

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, ie setting symbol7 = symbol1 etc? Right now it simply omits the points with factor level > 6.

绘图符号

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 (ie 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 .

When you have more than 6 factors it becomes difficult to differentiate in the final graph. I used a combination of colors and shapes to get the intended effect for 15 factors dividing them into 3 colors and 5 symbols with the following code after the geom_ lines

 +scale_color_manual(values=c(rep("#00BFCC",5), rep("#E7AA00",5), rep( "#FC4E07", 5)))  
+scale_shape_manual(values=rep(0:4, 3))

Also note that the values can be changed with different colors and shapes.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM