ggplot2 fill color from data values

陌路散爱 提交于 2019-12-04 03:45:28

问题


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.

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:


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



来源:https://stackoverflow.com/questions/32131717/ggplot2-fill-color-from-data-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!