用渐变着色geom_histogram [英] Coloring a geom_histogram by gradient

查看:557
本文介绍了用渐变着色geom_histogram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

我试图绘制一个geom_histogram, b
$ b

This is what I'm trying to do:

library(ggplot2)
set.seed(1)
df <- data.frame(id=paste("ID",1:1000,sep="."),val=rnorm(1000),stringsAsFactors=F)
ggplot(df,aes_string(x="val",y="..count..+1",fill="val"))+geom_histogram(binwidth=1,pad=TRUE)+scale_y_log10()+scale_fill_gradient2("val",low="darkblue",high="darkred")

但是得到:

But getting:

任何想法如何通过定义的渐变将它着色?

Any idea how to get it colored by the defined gradient?

推荐答案

不确定您可以填写 val ,因为直方图的每个条形图代表点的集合。

Not sure you can fill by val because each bar of the histogram represents a collection of points.

然而,您可以填写分类箱使用 cut 。例如:

You can, however, fill by categorical bins using cut. For example:

ggplot(df, aes(val, fill = cut(val, 100))) +
  geom_histogram(show.legend = FALSE)

这篇关于用渐变着色geom_histogram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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