overlay normal curve over a histogram [duplicate] - r

This question already has answers here:
Overlay normal curve to histogram in R
(4 answers)
Closed 4 years ago.
I am supposed to draw an overlay normal curve over a histogram in R. I am using the following code.
g <- unesco$Infant.Deaths
hist(g)
lines(seq(0, 200, by=5), dnorm(seq(0, 200, by=5),
mean(g), sd(g)), col="blue")
But instead of a curve, I am getting a straight line

It can be, that your histogram is representing frequencies instead of probability densities. Try to use hist(g, freq = FALSE).

Related

Normal density curve [duplicate]

This question already has answers here:
How to plot a function curve in R
(7 answers)
R plot Probability Density Function
(1 answer)
Distributions and densities in R [duplicate]
(1 answer)
Closed 4 months ago.
I was trying to plot a probability density function curve in R for normal distribution. I have used the following codes but am not getting the normal curve shape in the graph. Anyone can help? This is the code.
#generating normal variables using R
x=rnorm(10,mean=0,sd=0.5)
x
y <- dnorm(rnorm(, mean = 0, sd = 0.5)
plot(x,y, type = "l")

Histogram and distribution function in R [duplicate]

This question already has answers here:
Fitting a density curve to a histogram in R
(7 answers)
Closed 4 years ago.
i would like to plot a histogram of a dataset and the plot of the fitting lognormal distribution - I only see the histogram and not the distribution function...
hist(kmugesamt, prob=TRUE)
curve(plnorm(x, mean=mean(kmugesamt), sd=sd(kmugesamt)), add=TRUE)
Many thanks in advance
Does this help?
k<-rlnorm(1000)
hist(k, freq=F)
curve(dlnorm(x, mean=mean(k), sd=sd(k)), add=TRUE)

How to change the Scale of Y-Axis in R [duplicate]

This question already has answers here:
R Language: How to Set ylim?
(2 answers)
Closed 4 years ago.
I have this graph which I'm plotting
As you can see, the values on the black line which I'm trying to plot is not on the same scale as that of the red line.
Therefore, I'm trying to change the scale of the Y-Axis, so I can essentially "Zoom Out". How do I do this?
What I did till now is:
plot(d,vol1, type="l",xaxt="n", xlab="Date", ylab="Volatility Estimate", main="Nasdaq Pharmaceutical Index")
months= seq(min(d), max(d), "month")
axis(1, months, format(months, "%Y\n%b"))
lines(d, vol1, col="black")
You can add the argument ylim=c(a,b) inside the plot() command, where a is the minimum and b is the maximum of your desired y-axis.

How do I create a histogram in R with logarithmic x and y axes? [duplicate]

This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.

Adding a best fit normal on top of a histogram in R [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Fitting a density curve to a histogram in R
I'm trying to add a best fit normal over a histogram in R. Right now, I have the following:
x<-table[table$position==1,]$rt
hist(x,breaks=length(x))
And now I'd like to plot a normal curve over this plot which allows for skew and for kurtosis. How can I do this? This is what my curve looks like:
I would suggest not using the terms "skew" and "kurtosis" in the same sentence with "Normal curve", since Normal curves have neither. Perhaps you are looking for one or two parameter continuous density distribution that might be comparable to a function that was bounded at zero and had right skewing? If so then you should think about a) posting the data, b) consider plotting a Poisson, a log-Normal, or a gamma density on top of a histogram.
set.seed(123)
xpois <- trunc(rpois(100, 4))
hist(xpois)
lines(seq(0,10), 100*dpois(seq(0,10), 4))

Resources