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" )
