1

My question is about presentation as the following code

My partner did it with python and the bars were more and the graph was higher without adjusting as I have done here it is possible to do the same in R

grafica en python

Mine doesn't look anything like how can I do that aesthetic I've tried scales even without adjusting the bins and binwith but still can't get the consistency they want

My code:

library(dplyr)
library(ggplot2)
m1<-as.data.frame(replicate(100, rnorm(49,2,3))) 
mm1<-m1 %>% summarise_if(is.numeric, mean) #calcula la media del dataframe
rownames(mm1)="medias" #cambia el nombre de 1 por  "medias"
mm1<-t(mm1) #transpone el dataframe
mm1<-as.data.frame(mm1) #lo convierte en un dataframe
h0<-ggplot(mm1,mapping =   aes(x=medias)) #ingresa los datos para graficar
h1<-h0+geom_histogram(fill="aquamarine4",aes(y=2*(..density..)/sum(..density..)),binwidth =0.45 ,color="white") 
h2<-h1+stat_function(mapping = aes(colour = "N(2,3/7)"),fun = dnorm,args = list(mean = 2,sd = 3/7))
h3<-h2+stat_function(mapping = aes(colour = "N(2,3"),fun = dnorm,args = list(mean = 2,sd = 3))
h4<-h3 +labs(x = "Medias", y= "Escala",title = "Grafico para muestras tamaño 49")
h4
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Does this answer your question? [Creating a density histogram in ggplot2?](https://stackoverflow.com/questions/21061653/creating-a-density-histogram-in-ggplot2) – Ian Campbell Jun 17 '20 at 22:40

1 Answers1

1

Would be good to have the python code instead of just an image. When you calculate density, the density value is determined by the bin width. In the python plot, the bins run from 0 to 4, so you need to set that and call ..density.., like this in R:

h0 + geom_histogram(aes(y=..density..)) + 
stat_function(aes(colour = "N(2,3/7)"),fun = dnorm,args = list(mean = 2,sd = 3/7))+
stat_function(aes(colour = "N(2,3"),fun = dnorm,args = list(mean = 2,sd = 3)) + xlim(c(0,4))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • If I have it, it's jupiter, I just don't know how to upload it, it's a bit long ... note what happens is that I follow that to superimpose another graph, I must normalize `https://www.itl.nist.gov/div898/handbook/eda/section3/histogra.htm#:~:text=This%20is%20the%20intuitive%20case,histogram%20is%20equal%20to%20one` – Yeferson Gomez Jun 17 '20 at 17:20
  • hey @YefersonGomez, Did i answer your question? – StupidWolf Jun 17 '20 at 17:57
  • Yes thanks for help – Yeferson Gomez Jun 17 '20 at 20:57