简体   繁体   English

当超过 6 个因子水平时循环通过点形状

[英]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:geom_point使用美学映射shape时,当存在的因子数量超过 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." “形状调色板最多可以处理 6 个离散值,因为超过 6 个变得难以区分;您有 15 个。考虑手动指定形状。如果您必须拥有它们。”

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?有没有一种优雅的方法让 ggplot 在形状中循环,即设置 symbol7 = symbol1 等? Right now it simply omits the points with factor level > 6.现在它只是省略了因子水平 > 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:当您达到 >6 时,您必须手动设置数字,如下所示:

    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.正如 Luchonacho 指出的那样,有许多新的形状可用。 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.请记住,如果您想使用循环设置它们,请不要使用aes()因为它会暂时将最后一个绘图参考(即仅最后一个循环输入)保留在内存中,并且只绘制该参考。

在此处输入图片说明

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.如果您没有超过 6 个,那么手动选择它们很容易。 For example, if you have 10 lines, one alternative is:例如,如果您有 10 行,另一种选择是:

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 .正如其他答案所指出的,您需要使用scale_shape_manual

To repeat the desired symbols you can simply use rep(x, times) .重复所需的符号,您可以简单地使用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:例如,如果您想重复填写的符号 14 到 18(有关符号列表,请参阅 luchonacho 答案),您可以使用以下内容:

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 .这会将符号 15 到 18 重复五次,因此对于z 20 个不同值就足够了。

When you have more than 6 factors it becomes difficult to differentiate in the final graph.当您有 6 个以上的因素时,在最终图表中很难区分。 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我使用颜色和形状的组合来获得 15 个因素的预期效果,将它们分成 3 种颜色和 5 个符号,在 geom_ 行后使用以下代码

 +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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Dplyr过滤3个以上级别的因素时出现错误消息 - Error message when using Dplyr to filter with more than 3 levels to a factor R-列出因子2的水平超过2的因子1的水平 - R - Listing the levels of factor 1 that have more than 2 levels of factor 2 当训练集具有比测试集更多不同的因子水平时,randomForest不起作用 - randomForest does not work when training set has more different factor levels than test set 按因子水平对观察值进行子设置,且超过x个观察值 - Sub setting observations by factor levels with more than x observations 具有比R中的观察更多级别的因子的数据帧 - Dataframe with a Factor that has More Levels than Observations in R R:预测因子超过52个水平,被截断以打印输出 - R:More than 52 levels in a predicting factor, truncated for printout 是否有一种更简单的方法可以在 data.frame 中创建具有 2 个以上级别的因子变量? - Is there a simpler way to create a factor variable with more than 2 levels in a data.frame? 结果中的一个或多个因子水平没有数据:'' - One or more factor levels in the outcome has no data: '' 在大型文件中更有效地替换因子级别 - Replacing factor levels more efficiently in a huge file 通过循环遍历 dataframe 中的因子水平来计算相关性 - Compute the correlation by looping through factor levels in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM