简体   繁体   English

如何将x轴标签从字符更改为其他字符?

[英]How to change the x-axis labels from a character to a different one?

I've got a questions regarding the xaxis labels. 我对xaxis标签有疑问。 In contrast to whats discussed in here: 与此处讨论的内容相反:

How to specify the actual x axis values to plot as x axis ticks in R 如何指定实际的x轴值以绘制为R中的x轴刻度

I plotted a dataframe containing 10 columns. 我绘制了一个包含10列的数据框。 Each one is represented in a boxplot. 每个方框都用一个箱形图表示。 For the x-axis my labels are Pipe1 till Pipe 10. Now I wanna change those labels to a specific ID for instance this way 对于x轴,我的标签是Pipe1到Pipe10。现在我想将这些标签更改为特定的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"))

everytime I'm doing so, I receive an error revealing the following: 每次这样做,我都会收到一条错误消息,提示以下内容:

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

What did I do wrong in here? 我在这里做错了什么? I'd highly appreciate hints or advices. 我非常感谢提示或建议。 Cheers, Olli 干杯,奥利

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

Example with 2 columns 2列示例

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

在此处输入图片说明

Just to build on Djacks answer to explain what is happening, R is plotting an axis on a numerical scale, and then applying the labels pipe 1 etc. to those by default. 只是为了以Djacks的答案为基础来解释发生了什么,R在数字刻度上绘制了一个轴,然后默认将标签管道1等应用于那些。 You need to first suppress the default text labels using xaxt = "n" in your boxplot function (notice at this point that it is still producing the plot with an unlabelled x-axis) and then tell it to apply your chosen labels at the chosen locations using labels and at respectively in the axis function, with at = 1:10 . 您需要首先在xaxt = "n"函数中使用xaxt = "n"取消显示默认文本标签(此时注意,它仍在使用未标记的x轴生成图),然后告诉它在选定的位置应用您选择的标签在axis函数at分别使用labelsat位置, at = 1:10

Further illustrating this point that the axes are using a numerical coordinate system, you could plot text on the plot in line with the 3rd box using text("abc", x = 3, y = 0) . 为了进一步说明这一点,即轴正在使用数字坐标系,您可以使用text("abc", x = 3, y = 0)在与第三个框一致的绘图上绘制文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM