1
votes

I'm trying to plot some numbers in a graph, in the line x and y. The problem is I'm formatting the numbers as 3M for 3.000.000, 10k for 10.000, then I'm trying to put these numbers in the graph using the axis function, the problem is that I'm getting the message "Non-numeric argument to binary operator"

x<-c(10000,20000,50000,200000)
y<-c(24679826,99532203,623224134,1422415645)
x1<-paste(format(round(x / 1e3, 1), trim = TRUE), "K")
y1<-c(paste(format(round(y / 1e6, 1), trim = TRUE), "M"))

options(scipen=999)
plot(planilha2$N,planilha2$Iteracoes_Insercao,type="b",
       xlab="Tamanho dos Vetores",
       ylab="Numero de iterações",
       xaxt="n",yaxt="n",pch=16,col="red",lwd=2.2,
       main="Inserção iterativo (Vetores gerados aleatoriamente)", 
       cex.main=1)

axis(1, (paste(format(round(x1 / 1e3, 1), trim = TRUE), "K")))
axis(2,paste(round(y / 1e6, 1), trim = TRUE), "M")

enter image description here

1

1 Answers

0
votes

Several things to address here:

First, the error you're receiving is telling you this: You are applying some function, which requires a numeric value, to a value that is not numeric. In this case it is: x1/ 1e3. x1 is set a few lines earlier to a character vector. It is a group of character strings, so you cannot divide it by 1e3. I am assuming this is a typo, and you meant x instead of x1.

Next, you are missing some arguments to the axis() function. You need to tell axis() which points to draw the labels and what labels to draw. See the documentation for the axis() function.

To help you out, I fixed the code so that it draws a plot here:

x<-c(10000,20000,50000,200000)
y<-c(24679826,99532203,623224134,1422415645)

options(scipen=999)
plot(x, y,type="b",
     xlab="Tamanho dos Vetores",
     ylab="Numero de iterações",
     xaxt="n",yaxt="n",pch=16,col="red",lwd=2.2,
     main="Inserção iterativo (Vetores gerados aleatoriamente)", 
     cex.main=1)

axis(1, x,  labels = paste(format(round(x / 1e3, 1), trim = TRUE), "K"))
axis(2, y, labels = paste(format(round(y / 1e6, 1), trim = TRUE), "M"))

But, you will probably need to do some reading/experimentation to understand why I made the changes I did. For example, in your code you gave example vectors x and y, but then plotted columns from the data frame planilha2. Can you see why I had to change the plot() function in order for your example to run?