R:添加自定义刻度线标签
R: adding custom tick marks labels
set.seed(3)
y = rnorm(10)
x = seq(1, 10, 1)
plot(y ~ x)
如何添加自定义标签而不是 1、2、3、4、5 的 x 轴刻度标签?
假设我希望刻度线 1 被标记为 "This is a very long string 1",刻度线 2 被标记为 "This is a very long string 2" ... 等等。由于这些标签很长,我想将它们设置成一个角度(也许 135 度或类似的东西),这样它们就很容易阅读。我如何在 R 中执行此操作?
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?
这里有两个部分,轴上的自定义注释和旋转它们。
# 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()
请注意,页边距没有足够的空间来容纳长文本。所以在某些时候你会需要像 par(mar=c(6,1,1,1))
这样的东西。然后par(las=foo)
方式只能旋转90度。我确信 135 度是可能的,但不知道具体如何。 (也许 ggplot2 比基础图形更容易。)如果你想在 2 或 3 行中有你的长标签,那么你可以在字符串的中间添加 \n
's,例如。 "yo ho ho\nand a bottle of \nrum"
.
set.seed(3)
y = rnorm(10)
x = seq(1, 10, 1)
plot(y ~ x)
如何添加自定义标签而不是 1、2、3、4、5 的 x 轴刻度标签? 假设我希望刻度线 1 被标记为 "This is a very long string 1",刻度线 2 被标记为 "This is a very long string 2" ... 等等。由于这些标签很长,我想将它们设置成一个角度(也许 135 度或类似的东西),这样它们就很容易阅读。我如何在 R 中执行此操作?
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?
这里有两个部分,轴上的自定义注释和旋转它们。
# 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()
请注意,页边距没有足够的空间来容纳长文本。所以在某些时候你会需要像 par(mar=c(6,1,1,1))
这样的东西。然后par(las=foo)
方式只能旋转90度。我确信 135 度是可能的,但不知道具体如何。 (也许 ggplot2 比基础图形更容易。)如果你想在 2 或 3 行中有你的长标签,那么你可以在字符串的中间添加 \n
's,例如。 "yo ho ho\nand a bottle of \nrum"
.