R plot with strings showing in the axis

11,875

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

Share:
11,875
user2498497
Author by

user2498497

Updated on June 14, 2022

Comments

  • user2498497
    user2498497 almost 2 years

    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?