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

时光总嘲笑我的痴心妄想 提交于 2019-12-25 04:21:23

问题


I've got a questions regarding the xaxis labels. In contrast to whats discussed in here:

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

I plotted a dataframe containing 10 columns. 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

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


回答1:


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

Example with 2 columns

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




回答2:


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. 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.

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).



来源:https://stackoverflow.com/questions/50201944/how-to-change-the-x-axis-labels-from-a-character-to-a-different-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!