我试图用百分比来模拟密度图,但不幸的是很难找到正确的格式。我提供了下面的代码,尝试使用百分比来模拟密度图。
p <-
ggplot(mtcars, aes(x=mpg, y = ..count../sum(..count..))) +
#geom_density() alpha=.4, stat = 'bin', binwidth = "3000", position = "identity", fill="grey",cex = 0.8) +
geom_histogram(aes(x=mpg, y=..count../sum(..count..)), colour="black", fill="white", cex = 0.8)+
#geom_density( stat = "density", position = "identity", fill="grey",cex = 0.8)
geom_density(stat = 'bin', position = "identity", fill="grey",cex = 0.8)
p
Run Code Online (Sandbox Code Playgroud)
尝试使箱体的密度与曲率相似。但使用密度,它提供了下图:
任何帮助,将不胜感激。谢谢!
可能合适的一种选项是..density..特殊变量,然后是percent_formatfrom scales。
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=mpg)) +
geom_histogram(aes(y = ..density..), position = "identity",
colour="black", fill="white", cex = 0.8) +
geom_density(fill="grey",cex = 0.8, alpha = 0.5) +
scale_y_continuous(labels = percent_format(accuracy = 1))
Run Code Online (Sandbox Code Playgroud)