I have a question concerning plots in R using the baseplot-system: I want to plot 2 density plots in 1 plot. For this they need to have the same scales. I managed to change the scales so that I can plot them into 2 different plots. Thats exactly how I want them to look like:
mexp = NULL
for (i in 1 : 1000) mexp = c(mexp, mean(rexp(1000, 0.2)))
hist(mexp, col="lightblue", main = "Averages of 40 exponentials")
plot(density(rnorm(1000,mean= 5)), bw=11,col="red" )
plot( density( mexp , frequency(F) ), col="green") # frequency(F) adjusts the y-Axis
When I try to plot them into 1 plot it doesn`t work: I see the first plot only. I did this:
plot(density(rnorm(1000,mean= 5)), bw=11,col="red" )
line( density( mexp , frequency(F) , col="green" ))
Can anybody help me ? Thank you!
This code is better, but still contains several nonsense items.
First of all, it looks like you are comparing the wrong distributions. Your mexp data looks plausible. However, it looks like you are trying to compare it to a normal distribution with mean=5 and standard deviation=1. The mean is very close to the mean of your mexp data, but the mexp data has a mean of 0.157. You will not get much of a match like that.
Next the code has some mistakes and some absolute rubbish. Your first plot is:
Aside from the fact that you have an inappropriate standard deviation for the normal distribution, you have bw=11 as an argument for
plot
, which no such argument.bw
is an argument fordensity
but this is probably not a good choice for bw on this data. Let's just plot it using the default choice ofbw
and adjust that later if needed. So a better version of your first curve would be:Next, you have
There is a function
line
, but that is not what you want here. You are looking forlines
. Withlines
you are plotting another density. The argumentcol="green"
is misplaced. You have it as an argument todensity
when it should be an argument forlines
. And then there isfrequency(F)
. This is gibberish. Here F will be treated as the logical value FALSE.frequency
will consider that as a time series and find the frequency so that will give the value 1. You are then supplying that as the second argument ofdensity
so it will be treated as a bandwidth. This is total nonsense. Let's just delete it. That leaves us withlines(density(mexp), col="green")
Putting this all together, we get:This may not be exactly what you need but will be a better starting point.