Search code examples
rplotbar-chartgaussian

how to normalize a barplot to normal distribution in R?


I created a barplot using ggplot in which the x-axis represents a number in acending order and each number has is own percentage. I would like to normalized the observed barplot (Gaussian) in order to compare the two barplots. someone know how to do it? Here is my code:

lemon_imp_05 = subset(Lemon_brevante_data,Lemon_brevante_data$Block == "IMP-05")
  S = lemon_imp_05$percentage
  names(S) = lemon_imp_05$count
  barplot(S, main = 'Block IMP-05 Loam',
          xlab= 'Count(N.fruits/carton)',ylab = 'percentage(%)', col = "green")

The result is a non normal distribution bar plot, but I want to force a normal distribution on the observed barplot (don't know how to add picture of the current result). any suggestions? Thanks


Solution

  • Do you want to plot the densitity distribution of your empirical data, and compare it to a normal distribution with same mean and sd?

    plot(density(y), ylim=c(0, .5), lwd=2)
    curve(dnorm(x, mean=mean(y), sd=sd(y)), add=TRUE, lwd=2, lty=2, col=2)
    

    enter image description here

    Like this?


    Data:

    set.seed(42)
    y <- rnorm(1e2, mean=1.5, sd=.8)
    

    Or use hist.

    h <- hist(y, freq=F, breaks="FD")
    curve(dnorm(x, mean=mean(y), sd=sd(y)), add=TRUE, lwd=2, lty=2, col=2)
    

    enter image description here

    ## stored values
    h
    # $breaks
    # [1] -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5
    # 
    # $counts
    # [1]  2  2  6 15 21 23 17 11  3
    # 
    # $density
    # [1] 0.04 0.04 0.12 0.30 0.42 0.46 0.34 0.22 0.06
    # 
    # $mids
    # [1] -0.75 -0.25  0.25  0.75  1.25  1.75  2.25  2.75  3.25
    # 
    # $xname
    # [1] "y"
    # 
    # $equidist
    # [1] TRUE
    # 
    # attr(,"class")
    # [1] "histogram"