简体   繁体   中英

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

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

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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