我想使用频率和百分比直方图比较变量的分布。换句话说,我想看看频率直方图中每个bin有多少百分比。
#load data
j<-as.data.frame(Nile)
#frequency plot
ggplot(j, aes(x = x))+
geom_histogram( fill = "black", binwidth = 200, alpha=0.8)+
scale_x_continuous(breaks =seq(400,1400,200))
创建百分比直方图
#percentage plot
ggplot(j, aes(x = x), fill = "black", binwidth = 200, alpha=0.8)+
geom_histogram( aes(y= (..count..)/sum(..count..)*100))+
scale_x_continuous(breaks =seq(400,1400,200))+
ylab("Percentages")
然而,它给了我一个警告stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
,它有一个我期望的不同直方图
我尝试了以下
ggplot(j, aes(x = x), fill = "black", alpha=0.8)+
geom_histogram( aes(y= (..count..)/sum(..count..)*100))+
scale_x_continuous(breaks =seq(400,1400,200))+
ylab("Percentages") +
stat_bin(bindwidth=200)
但是,我得到了相同的第二个直方图,默认的binwidth和相同的警告
答案 0 :(得分:2)
您错误地拼写了binwidth
。你拼写为bindwidth
ggplot(j, aes(x = x), fill = "black", alpha=0.8)+
geom_histogram(aes(y= (..count..)/sum(..count..)*100), binwidth = 200) +
scale_x_continuous(breaks =seq(400,1400,200)) +
ylab("Percentages")
答案 1 :(得分:0)
这似乎是一个语法错误。正确拼写binwidth将会解决。