Problem with a for loop for plot

Hello there,
I´m programming a code to plot some point of a dataset, and I get the length error, dont know why and how to fix it, is a "for" loop and here is the code:

for (i in Data) { 
    if (proveedor == 1) {plot(coorx[i], coory[i], col = "yellow")} else
    if (proveedor == 2) {plot(coorx[i],coory[i], col = "red")} else 
    if (proveedor == 3) {plot(coorx[i],coory[i], col="green")}else
    if (proveedor == 4){plot(coorx[i], coory[i], col = "blue")} else
    if (proveedor == 5) {plot(coorx[i], coory[i], col = "violet")}else
    if (proveedor == 6) {plot(coorx[i], coory[i], col = "grey")} else NULL
}
#> Error in eval(expr, envir, enclos): object 'Data' not found

I also attach an image of my data, so the code gets more contextualized, I'm pretty new in this language so I would be son thankful if u can answer me, THANKS!

Created on 2019-10-29 by the reprex package (v0.3.0)

Welcome Spectral!

The error says there's no object called Data in the R environment.

Assuming you've loaded your data into R and the data frame is called Data, then the code for the loop would be for (i in 1:nrow(Data)) {. The loop is supposed to go through each row of the data, so the loop variable i should go from 1 to the number of rows in the data.

You would also need to provide the data frame name for the plot command (e.g., plot(Data$coorx[i], Data$coory[i], col="yellow")), but (as raytong shows below) you should run plot only once and use points to add the points. In any case, a more efficient approach would be something like this (using the built-in mtcars data frame for the example):

# Set the color palette
palette(c("yellow", "red", "green"))

d = mtcars
plot(d$mpg, d$hp, col=d$cyl)

You might also find ggplot2 package useful:

library(ggplot2)

ggplot(mtcars, aes(hp, mpg, colour=cyl)) + 
  geom_point() + 
  scale_colour_manual(values=c("red", "purple", "orange"))
1 Like

Hi @Spectral. You can use ggplot to do your plot easier. But if you want to code it with for loop. Try the following.
Your code cannot be run because the for (i in Data) will just loop each column, not row. And the you call the column name cannot get the values inside. The if else statement use plot will refresh all plot and cannot plot on each others. So, for your code, I suggest use with to construct the environment with the column as variable. And start an empty plot with plot. Overlay point will points.

``` r
Data <- data.frame(proveedor = rep(1:5, 3),
           coorx = rnorm(15),
           coory = rnorm(15))

with(Data, {
  plot(1, type = "n", xlim = range(Data$coorx), ylim = range(Data$coory))
  for (i in 1:nrow(Data)) { 
  if (proveedor[i] == 1) {points(coorx[i], coory[i], col = "yellow")} else
    if (proveedor[i] == 2) {points(coorx[i],coory[i], col = "red")} else 
      if (proveedor[i] == 3) {points(coorx[i],coory[i], col="green")}else
        if (proveedor[i] == 4){points(coorx[i], coory[i], col = "blue")} else
          if (proveedor[i] == 5) {points(coorx[i], coory[i], col = "violet")}else
            if (proveedor[i] == 6) {points(coorx[i], coory[i], col = "grey")} else NULL
}})

Created on 2019-10-29 by the reprex package (v0.3.0)

2 Likes

@raytong
Dont get what u do with the

Data <- data.frame(proveedor = rep(1:5, 3),
           coorx = rnorm(15),
           coory = rnorm(15))

Can u explain me what are u doing? I´m from Chile so if u can keep the explanation simple (in terms of english) and also don´t understand what is "with" function doing, but that's on me, by the way thank u!

Thank u, I'll get more info about ggplot2 and apply it in another time, really thanks, but by now is more usefull the answer wich is more similary to my code, THANKS AGAIN!

raytong's code creates fake data so that he has some data to use for his example. If you run his code, you can see the data frame that get's created.

The with function allows you to use column names without having to repeat the name of the data frame. For example, compare these:

list(mpg=mean(mtcars$mpg), range.mpg=range(mtcars$mpg), range.hp=range(mtcars$hp))

with(mtcars, list(mean=mean(mpg), range.mpg=range(mpg), range.hp=range(hp)))
2 Likes

I understand, you're right, so how can I process my data to get use of it without the warning of length error

Just figure it out, works perfectly and I understand all the code, thank u guys @raytong

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.