R 绘制百分比而不是密度

R 绘制百分比而不是密度,r,ggplot2,R,Ggplot2,我试图用百分比模拟密度图,但不幸的是难以找到正确的格式。我提供了下面的代码,试图使用百分比模拟密度图 p <- ggplot(mtcars, aes(x=mpg, y = ..count../sum(..count..))) + #geom_density() alpha=.4, stat = 'bin', binwidth = "3000", position = "identity", fill="grey",c

我试图用百分比模拟密度图,但不幸的是难以找到正确的格式。我提供了下面的代码,试图使用百分比模拟密度图

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

p一个合适的选项是
.density..
特殊变量,然后是
比例中的
百分比格式

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))