Reputation: 9793
set.seed(3)
y = rnorm(10)
x = seq(1, 10, 1)
plot(y ~ x)
Instead of the x-axis tick mark labels of 1, 2, 3, 4, 5, how can I add a custom label? Suppose I want tick mark 1 to be labeled as "This is a very long string 1", tick mark 2 to be labeled as "This is a very long string 2" ... etc. Since these labels are long, I'd like to set them at an angle (maybe 135 degrees or something like that) so that they're easy to read. How can I do this in R?
Upvotes: 3
Views: 9094
Reputation: 6459
Instead of the x-axis tick mark labels of 1, 2, 3, 4, 5, how can I add a custom label? Suppose I want tick mark 1 to be labeled as "This is a very long string 1", tick mark 2 to be labeled as "This is a very long string 2" ... etc. Since these labels are long, I'd like to set them at an angle (maybe 135 degrees or something like that) so that they're easy to read. How can I do this in R?
You have two parts here, custom annotations on axis, and rotating them.
# First turn off axes on your plot:
plot(1:5, 1:5, axes=FALSE)
# now tell it that annotations will be rotated by 90* (see ?par)
par(las=2)
# now draw the first axis
axis(1, at=1:5, labels=c("yo ho ho and a bottle of rum", 2:5))
# add the other default embellishments, if you like
axis(2) #default way
box()
Notice that there won't be enough room on margins to fit a long text. So at some point you will need something like par(mar=c(6,1,1,1))
. And then the par(las=foo)
way can only rotate it by 90 degrees. I'm sure 135 degrees would be possible but don't know exactly how. (Maybe it would be easier with ggplot2 than base graphics.) And if you want to have your long label in 2 or 3 rows, then you can add \n
's in the middle of the string eg. "yo ho ho\nand a bottle of \nrum"
.
Upvotes: 2