geom_text()&颜色渐变

时间:2017-11-15 19:34:02

标签: r ggplot2 geom-text

我希望在geom_text函数中设置一个颜色渐变,从绿色为低,红色为高。我的代码如下:

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

我正在寻找类似的东西:

What I am looking for

1 个答案:

答案 0 :(得分:1)

将颜色映射到数字变量 - 例如这会通过在y中指定color=y将颜色映射到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")