César Troya
César Troya

Reputation: 11

R programming "Col="red"" error in plot

I'm trying to make a plot in R, the part of getting the drawing of the given values works correctly, I'm getting this: plot without color

Using this code:

g<- function(x,y)
  + x**2+y**2-3*x+y+2
gb<- function(x) g(x[1],x[2])
x <- seq(-2,2,len=51)
y <- seq(-2,2,len=51)
gz<-outer(x,y,g)
contour(x,y,gz)
polygon(c(0,0,1),c(1,2,1))

but when I want to add color to the polygon with this code:

 g<- function(x,y)
  + x**2+y**2-3*x+y+2
gb<- function(x) g(x[1],x[2])
x <- seq(-2,2,len=51)
y <- seq(-2,2,len=51)
gz<-outer(x,y,g)
contour(x,y,gz)
polygon(c(0,0,1),c(1,2,1),
        + col="red",
        +density=c(30,40))

I get the following error:

Error: unexpected '=' in:
"polygon(c(0,0,1),c(1,2,1),
        + col="

The only package I am using for my program is: library(mosaicCalc)

Upvotes: 0

Views: 217

Answers (1)

KenHBS
KenHBS

Reputation: 7164

The error message tells you there is some mistake somewhere in the polygon(), so that's the line we'll focus on.

You pasted:

polygon(c(0,0,1),c(1,2,1),
    + col="red",
    +density=c(30,40))

Which is the same as

polygon(c(0,0,1),c(1,2,1), + col="red", +density=c(30,40))

The + are not supposed to be there and they are causing the error. Use

polygon(c(0,0,1),c(1,2,1), col="red", density=c(30,40))

and you should be fine.

Sidenote: It seems like you have copy-pasted this code from somewhere and then ran into this error when running it yourself. If someone copies code from their console, it usually includes a + if multiple lines of code belong to the same statement. As you've learnt now, you should remove those before you run the code.

Upvotes: 2

Related Questions