R plot with strings showing in the axis

user2498497 picture user2498497 · Jun 27, 2013 · Viewed 11.4k times · Source

I am plotting number of residents against the dorm room numbers (4 digits). The room numbers are supposed to be strings. But when I used as.character(RmNum), the axis still shows as numeric.

meanResidents = c(3, 4, 3, 2, 4, 5)
rmNumber = c(2034, 3043, 4012, 2035, 2022, 3013)
plot(as.character(rmNumber), meanResidents, xlab = as.character(rmNumber))

I would want the dorm numbers showing vertically in the axis. Can someone help me with that?

Answer

Ricardo Oliveros-Ramos picture Ricardo Oliveros-Ramos · Jun 27, 2013

With the function axis you can specify the position of the axis, where to put the tick marks (at) and choose the labels. The parameter las=2 means labels perpendicular to the axis.

plot(meanResidents, axes=FALSE, xlab="dorms")
axis(2)
axis(1, at=seq_along(meanResidents),labels=as.character(rmNumber), las=2)
box()

the plot