2015-08-21 106 views
1

我希望能夠根據存儲在數據框中的值設置ggplot填充顏色。下面的代碼幾乎是我想要做的,除了代替僅使用fill = MyColor,我希望代碼實際使用MyColor字段中的RRGGBB十六進制值。ggplot2從數據值填充顏色

df = data.frame(Animals = c("Dog", "Cat", "Horse", "Giraffe"), 
      Number = c(88, 11, 107, 59), 
      MyColor = c("FFFFFF", "D9FFFF", "CC80FF", "FFB5B5")) 

p <- ggplot(df) 
p <- p + aes(x = Animals, y = Number, fill = MyColor) 
p <- p + geom_bar(stat = 'identity') 
print(p) 

感謝,

保羅

回答

3
ggplot(df, aes(Animals, Number, fill=Animals)) + geom_bar(stat='identity') + 
    scale_fill_manual(values = df$MyColor) 

enter image description here

+1

我曾試圖scale_fill_manual但未能成功。我缺少填充=動物。謝謝!! – user3344266

+0

yw。樂於幫助 –