Reputation: 37
I am trying to mimic density plots with percentage, but unforunately having difficulty finding the right format. I have provided the code below trying to mimic density plots using percentage.
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
Trying to get the bins to be similar to a density with curvatures. But using density, it provides the plot below:
Any help would be appreciated. Thanks!
Upvotes: 1
Views: 1928
Reputation: 24790
One option that may be appropriate is the ..density..
special variable and then percent_format
from 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))
Upvotes: 5