Multicolored geom_line

I'm trying to draw some geom_lines on a ggplot where the color varies according to a boolean test.

According to r - geom_line - different colour in the same line - Stack Overflow the idea seems to be to add a test to the color parameter in aes, but when I put this in the geom_line(), rather than the ggplot() in the example, for instance:

geom_line( data=oneDateData, aes(x=Strike, y=TotalPutsOi/axis2Coeff), size=0, color=(Strike>=minPutItm))

I get:

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomLine, :
object 'Strike' not found

Thats probably not the issue as you can see by changing the example you linked to that geom_line can tolerate that approach:

time <- seq (1,7,1)
var1 <- c(3,5,7,2,3,2,8)
var2 <- c(2,4,18,16,12,3,2)
DF <- data.frame(time, var1, var2)

ggplot(DF, aes(time, var1)) + 
  geom_line(aes(group=1, colour=(var2>10)))

for specific help with your code and data, consider providing a reprex.

You're right: I should have tried that first. Hmm, I shall prod it further with the sharpened stick and see what ensues!

Many thanks.

Got it. Blasted typo. (Color=... wasn't in the aes).

Thanks though for showing me the fundamentals were correct.

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.