从数据值中填充颜色。

时间:2023-02-09 21:43:02

I want to be able to set the ggplot fill color from values stored in the data frame. The following code is 'almost' what I am trying to do, except that instead of just using fill = MyColor, I want the code to actually use the RRGGBB hex value in the MyColor field.

我希望能够从数据帧中存储的值设置ggplot填充颜色。下面的代码是我要做的,除了不使用fill = MyColor,我希望代码在MyColor字段中实际使用RRGGBB十六进制值。

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)

Thanks,

谢谢,

Paul

保罗

1 个解决方案

#1


4  

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

从数据值中填充颜色。

#1


4  

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

从数据值中填充颜色。