替换频率图上的标签

时间:2013-04-17 09:54:38

标签: r plot

我绘制了一个变量性别的频率表,其编码如下:女性:2,男性:3。

在情节中,我看到2和3,但我想把“女性”和“男性”,而不改变data.frame中的值,因为它需要更多的位置。

我该怎么做?

plot(table4, main="Frequency table",xlab= "Gender", ylab="Country" )

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,使用函数table()制作一些样本数据和计算频率表。

 df<-data.frame(Sex=sample(c(2,3),10,replace=TRUE),Country=sample(c(4,5),10,replace=TRUE))
 table4<-table(df)
 table4
   Country
Sex 4 5
  2 4 1
  3 2 3

使用功能str(),您可以看到table4的结构。 Sex级别2和3存储为维度名称。因此,您可以使用函数attr()替换它们并选择$Sex

 str(table4)
 'table' int [1:2, 1:2] 4 2 1 3
 - attr(*, "dimnames")=List of 2
  ..$ Sex    : chr [1:2] "2" "3"
  ..$ Country: chr [1:2] "4" "5"
 attr(table4,"dimnames")$Sex<-c("Female","Male")

现在改变了table4对象。

plot(table4,main="Frequency table",xlab= "Gender", ylab="Country" )

enter image description here

相关问题