gpt4 book ai didi

R:添加自定义刻度线标签

转载 作者:行者123 更新时间:2023-12-04 18:02:17 26 4
gpt4 key购买 nike

set.seed(3)
y = rnorm(10)
x = seq(1, 10, 1)
plot(y ~ x)

enter image description here

如何添加自定义标签而不是 1、2、3、4、5 的 x 轴刻度标签?假设我希望刻度标记 1 被标记为“这是一个很长的字符串 1”,刻度标记 2 被标记为“这是一个很长的字符串 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 度是可能的,但不知 Prop 体如何。 (也许 ggplot2 比基本图形更容易。)如果你想在 2 或 3 行中有你的长标签,那么你可以在字符串的中间添加 \n,例如. “哟呵呵呵\n和一瓶朗姆酒”

关于R:添加自定义刻度线标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com