ggplot2从数据值填充颜色 [英] ggplot2 fill color from data values

查看:955
本文介绍了ggplot2从数据值填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  df = data.frame(Animals = c(狗,猫,马,长颈鹿) ,
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)

谢谢,

Paul

解决方案

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


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

解决方案

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

这篇关于ggplot2从数据值填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆