I'm trying to produce a histogram with ggplot'
s geom_histogram
which colors the bars according to a gradient, and log10's them.
我正在尝试用ggplot的geom_histogram生成一个直方图,它根据渐变为条形图着色,log10就是它们。
Here's the code:
这是代码:
library(ggplot2)
set.seed(1)
df <- data.frame(id=paste("ID",1:1000,sep="."),val=rnorm(1000),stringsAsFactors=F)
bins <- 10
cols <- c("darkblue","darkred")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
df$cut <- cut(df$val,bins)
df$cut <- factor(df$cut,level=unique(df$cut))
Then,
然后,
ggplot(data=df,aes_string(x="val",y="..count..+1",fill="cut"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(df$cut))+
scale_fill_manual(values=cut.cols,labels=levels(df$cut))+
scale_y_log10()
得到:
whereas dropping the fill
from the aesthetics
:
从美学中删除填充物:
ggplot(data=df,aes_string(x="val",y="..count..+1"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(cuts))+
scale_fill_manual(values=cut.cols,labels=levels(cuts))+
scale_y_log10()
得到:
Any idea why do the histogram bars differ between the two plots and to make the first one similar to the second one?
知道为什么直方图条在两个图之间不同并且使第一个图与第二个图相似?
1 个解决方案
#1
2
The OP is trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient...
OP正试图用ggplot的geom_histogram生成一个直方图,根据渐变颜色调整条形...
The OP has already done the binning (with 10 bins) but is then calling geom_histogram()
which does a binning on its own using 30 bins by default (see ?geomhistogram
).
OP已经完成了binning(有10个bin),但是后来调用了geom_histogram(),它默认使用30个bin进行binning(参见?geomhistogram)。
When geom_bar()
is used instead together with cut
instead of val
当geom_bar()与cutinstead val一起使用时
ggplot(data = df, aes_string(x = "cut", y = "..count..+1", fill = "cut")) +
geom_bar(show.legend = FALSE) +
scale_color_manual(values = cut.cols, labels = levels(df$cut)) +
scale_fill_manual(values = cut.cols, labels = levels(df$cut)) +
scale_y_log10()
the chart becomes:
图表变为:
Using geom_histogram()
with filled bars is less straightforward as can be seen in this and this answer to the question How to fill histogram with color gradient?
使用带填充条的geom_histogram()不那么简单,如下所示,这个问题的答案如何用颜色渐变填充直方图?
#1
2
The OP is trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient...
OP正试图用ggplot的geom_histogram生成一个直方图,根据渐变颜色调整条形...
The OP has already done the binning (with 10 bins) but is then calling geom_histogram()
which does a binning on its own using 30 bins by default (see ?geomhistogram
).
OP已经完成了binning(有10个bin),但是后来调用了geom_histogram(),它默认使用30个bin进行binning(参见?geomhistogram)。
When geom_bar()
is used instead together with cut
instead of val
当geom_bar()与cutinstead val一起使用时
ggplot(data = df, aes_string(x = "cut", y = "..count..+1", fill = "cut")) +
geom_bar(show.legend = FALSE) +
scale_color_manual(values = cut.cols, labels = levels(df$cut)) +
scale_fill_manual(values = cut.cols, labels = levels(df$cut)) +
scale_y_log10()
the chart becomes:
图表变为:
Using geom_histogram()
with filled bars is less straightforward as can be seen in this and this answer to the question How to fill histogram with color gradient?
使用带填充条的geom_histogram()不那么简单,如下所示,这个问题的答案如何用颜色渐变填充直方图?