为什么我们需要先创建一个 table 然后将其作为参数传递给 R 中的饼图?

Why do we need to create a table first and then pass it as argument for pie plot in R?

这是我的一段代码:

myTable <- table(mtcars$am)  
pie(myTable)

table(交叉)将数据制表成 pie.

可以理解的内容

的确,(见?pie),pie需要x: a vector of non-negative numerical quantities. The values in x are displayed as the areas of pie slices.

如果您不使用 tablepie 将绘制尽可能多的(非 <= 0)切片。您可以阅读 ?pie 了解更多详情,并尝试以下操作:pie(sample(1:5, 12, replace=T)) 以及 example(pie).

来自帮助 ?pie 你有

x: a vector of non-negative numerical quantities. The values in ‘x’ are displayed as the areas of pie slices.

labels: one or more expressions or character strings giving names for the slices. Other objects are coerced by ‘as.graphicsAnnot’. For empty or ‘NA’ (after coercion to character) labels, no label nor pointing line is drawn.

所以你不需要传递 table 而是传递 vector of non-negvative values 除非你想在你的情况下总结数据。

table(mtcars$am) returns 两个标签的计数的 2x2 table(此处为 0 和 1):

>> table(mtcars$am)

   0  1 
   19 13 

>> mtcars$am

   1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1

table,或者更好的数据框,通常是一种处理数据的便捷方式,可以让所有与您正在处理的特定类型或主题相关的数据遍布各处以变​​量的形式。当您处理多个数据集时,这尤其方便。