-1

I plotted a frequency table of a variable gender which is coded like this : female :2, male: 3.

In the plot, I see 2 and 3, but I'd like to put "Female" and "Male, without changing the value in the data.frame, because it would take more place.

How can I do that?

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

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
Ricol
  • 377
  • 4
  • 9
  • 22

1 Answers1

0

First, made some sample data and calculated frequency table with function 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

With function str() you can see structure of table4. Sex levels 2 and 3 are stored as dimension names. So you can replace them using function attr() and selecting $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")

Now plot changed table4 object.

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

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201