wander_rythm -
wander_rythm -

Reputation: 37

Plotting Percentage rather than Density

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

enter image description here

Trying to get the bins to be similar to a density with curvatures. But using density, it provides the plot below:

enter image description here

Any help would be appreciated. Thanks!

Upvotes: 1

Views: 1928

Answers (1)

Ian Campbell
Ian Campbell

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

enter image description here

Upvotes: 5

Related Questions