0
我试图产生一个直方图ggplot'
s geom_histogram
根据梯度颜色的酒吧,和log10的他们。ggplot与填充和scale_y_log10结合的奇怪行为()
下面的代码:
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))
然后,
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()
而来自aesthetics
丢弃fill
:
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()
任何想法为什么直方图条不同之间的两个情节,并使第一个类似于第二个?
默认情况下'geom_histogram'使用'position_stack'。您可以将其更改为'position_identity',但您可能希望将这些条形成透明。我建议使用刻面。 – Roland