ggplot为十六进制颜色指定错误的颜色

时间:2015-08-07 13:40:27

标签: r ggplot2

我将df列中的十六进制颜色分配给ggplot。 但是在图中它显示的是十六进制颜色的名称,但没有显示正确的颜色,颜色与十六进制颜色不匹配。

数据框和绘图代码:

"transaction" : {
    "properties" :
    {
        "ts" : {
            "format" : "dateOptionalTime",
            "type" : "date"
        },
        "orig_ip" : {
            "type" : "ip"
        },
        "orig_port" : {
            "type" : "long"
            "ignore_malformed": true     <---- add this line
        }
   }
}

在dput重新编写的数据下面

str(Trun)
'data.frame':   1043 obs. of  12 variables:
$ X       : int  1 2 3 4 5 6 7 8 9 10 ...
$ DE      : num  36.5 37 40.2 36.8 38.8 ...
$ hex     : chr  NA NA NA NA ...

unique(Trun$hex)
[1] NA        "#8A8F8C" "#507085" "#3F7767" "#917652" "#DBAD5D"

ggplot(data=Trun, aes(x=X, y=DE, colour=hex)) + geom_point() + ggtitle("DE with 35* sd values")

1 个答案:

答案 0 :(得分:4)

我创建了一个包含十六进制值作为字符的新变量,并将该变量添加到aes中。

#create a new variable, substituting red for missings and turning factor into string (you can add your own colour)
Trun$hex2 <- ifelse(Trun$hex=="","#CC0000",as.character(Trun$hex))

#plot    
ggplot(data=Trun, aes(x=X, y=DE, colour=hex2)) + geom_point(size=3) +  #larger vor visibility
  ggtitle("DE with 35* sd values") +
  scale_colour_identity()

enter image description here