Search code examples
rggplot2geom-text

geom_text() & color gradient


I am looking to set a color gradient from green for low and red for high on the geom_text function. My code is as follows:

mydata <-  data.frame(R_Test_Data)

datatime <- mydata$TIMESTAMP
wind_speed <- mydata$WS_ms_Avg
wind_direction <-mydata$WS_ms_WVc

ggplot(data = mydata, aes(x = datatime, y = wind_speed))+
  geom_line() +
  geom_text(aes(angle=-wind_direction + 270), label="→", 
            colour = wind_speed, size = 7 ) +
 scale_colour_gradient(low="green", high="red") 

This is my result

I am looking for something kind of like this:

What I am looking for


Solution

  • Map colour to to a numeric variable - e.g. this maps color to y by specifying color=y within aes:

    set.seed(1);df <- data.frame(x = 1:10, y = 1:10, angle = runif(10,90,180))
    library(ggplot2)
    ggplot(df, aes(x,y)) + 
      geom_text(aes(angle=angle, color=y), label="-", size = 12) + 
      scale_colour_distiller(palette="RdYlGn")