ifelse inside image function

Hi, I am trying to use ifelse inside the image function as follows:

x <- y <- 1:5
z <- c(1:5, 2:6, 3:7, 4:8, 5:9)
z <- matrix(z, 5, 5)
image(x, y, z)
image(x, y, z, col = ifelse(z > 5, 'red', 'green'))

I would like to post both images but I'm a newbie so I can only post one. If you run the code, you should be able to see both images. The second one should look like this:

image2

However, I expect it to have red in the upper-right triangle and green along the diagonal and in the lower-left triangle. Do you know why my code isn't generating that? Thank you for your help.

Hello Shenan,

Welcome to the forum! If you want to generate both plots on your preview pane you can just add the below in front of your code.

par(mfrow=c(1,2)) 

x <- y <- 1:5
z <- c(1:5, 2:6, 3:7, 4:8, 5:9)
z <- matrix(z, 5, 5)
image(x, y, z)
image(x, y, z, col = if_else(z > 5, 'red', 'green'))

In terms of your second question - not exactly sure what it is you're asking. I am seeing red in the upper-right "triangle" and my diagonal is green (along with my lower-left triangle green). Able to elaborate here?

Hi, the upper-right triangle should be bigger and the lower-left triangle should not include any red. I attach here an image which shows what the triangles should look like. The colours are different (maroon = red and yellow = green) but it does show which cells should be red and green.

image3

Thank you for your help.

Ahh I see now what you mean. Interestingly enough the ifelse is working as intended as we can run that seperately and print it and it gives us the expected pattern above. Running it within the image function itself does not return the expected output. I'll have to dig a bit to see why.

x <- y <- 1:5
z <- c(1:5, 2:6, 3:7, 4:8, 5:9)
z <- matrix(z, 5, 5)
colours <- ifelse(z > 5, 'red', 'green') 
colours
#>      [,1]    [,2]    [,3]    [,4]    [,5]   
#> [1,] "green" "green" "green" "green" "green"
#> [2,] "green" "green" "green" "green" "red"  
#> [3,] "green" "green" "green" "red"   "red"  
#> [4,] "green" "green" "red"   "red"   "red"  
#> [5,] "green" "red"   "red"   "red"   "red"

Created on 2020-09-22 by the reprex package (v0.3.0)

Okay I finally have a solution for you. The post that help me was here: How to correctly use the color option in R image() function? Essentially, it needs to attribute colours in a specific way and depending on how it determines boundaries or breaks it will make a call in how it needs to assign the colour (even on your plot you can see that there are technically those which fall "in the middle". To circumvent this I assigned your qualifying condition to z as you'll see so we end with a matrix of 1's and 0's. Passing that then as our object with clear boundaries that the one condition should be green and the other red leaves no ambiguity. See below


x <- y <- 1:5
z <- c(1:5, 2:6, 3:7, 4:8, 5:9)
z <- matrix(z, 5, 5)

z[z<=5] <- 0
z[z>5] <- 1


image(z, col = c("green","red"))

Created on 2020-09-22 by the reprex package (v0.3.0)

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.