【问题标题】:How to change the x-axis labels from a character to a different one?如何将 x 轴标签从一个字符更改为另一个字符?
【发布时间】:2018-10-16 12:52:02
【问题描述】:

我有一个关于 xaxis 标签的问题。 与此处讨论的内容相反:

How to specify the actual x axis values to plot as x axis ticks in R

我绘制了一个包含 10 列的数据框。每一个都用箱线图表示。 对于 x 轴,我的标签是 Pipe1 到 Pipe 10。现在我想将这些标签更改为特定的 ID,例如这种方式

windows()
par(mfrow= c(2,1),las=3)
boxplot(output.valid.fast,outline=F, xlab ="Pipes",ylab="RMSE(-)")
axis(1,at=c("Pipe1","Pipe2","Pipe3","Pipe4","Pipe5","Pipe6","Pipe7","Pipe8","Pipe9","Pipe10"),labels=c("1234","2345","3456","4567","5678","6789","78910","891011","9101112","10111213"))

每次执行此操作时,我都会收到一条错误消息,显示以下内容:

In axis(1, at = c("Pipe1", "Pipe2", "Pipe3", "Pipe4", "Pipe5", "Pipe6",  :
  NAs introduced by coercion

我在这里做错了什么?我非常感谢提示或建议。 干杯, 奥利

【问题讨论】:

  • 非常感谢,就是这样!

标签: r plot axis axis-labels


【解决方案1】:

at = c("Pipe1", ... , "Pipe10") 替换为at = 1:10

2 列示例

boxplot(data.frame(Pipe1 = 1:10, Pipe2 = 2:11), xaxt = "n")
axis(1, at = 1:2, labels = c("1234","2345"))

【讨论】:

    【解决方案2】:

    只是建立在 Djacks 答案的基础上来解释正在发生的事情,R 正在以数字比例绘制一个轴,然后将标签 pipe 1 等应用于默认情况下。您需要首先在 boxplot 函数中使用 xaxt = "n" 抑制默认文本标签(注意此时它仍在生成带有未标记 x 轴的图),然后告诉它在所选位置应用您选择的标签使用labelsat分别在axis函数中,与at = 1:10

    为了进一步说明轴使用数字坐标系这一点,您可以使用text("abc", x = 3, y = 0) 在图上绘制与第三个框一致的文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 2018-05-20
      • 2011-02-19
      • 1970-01-01
      • 2018-06-19
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多