Sunday 15 August 2010

matlab - Plotting graphs on current barplot in R -



matlab - Plotting graphs on current barplot in R -

i'm looking technique in r similiar command hold all in matlab. in matlab generate data:

x = normrnd(0,1,1000,1); [a,b]=hist(x,20); l=b(2)-b(1); area=sum(l*a); frequency=a/area; bar(b,frequency,1); hold all; range=b(1):0.1:b(20); f1=normpdf(range,0,1); f2=normpdf(range,2,2); plot1=plot(range,f1,'r'); plot2=plot(range,f2,'m'); hold off;

i create similiar in r. i've tried way:

x <- rnorm(1000) h <- hist(x, breaks = 20) <- h$counts b <- h$mids l <- b[2] - b[1] area <- sum(l*a) frequency = a/area range <- seq(b[1],b[20], = 0.1) f1 <- dnorm(range,0,1) f2 <- dnorm(range,2,2) barplot(frequency, names.arg = c(b))

and stopped here, since don't know how add together graph current plot. tried utilize ggplot2, haven't much experience , failed on creating barplot library. if there way ggplot2, know explanation, since want larn it. appreciate solution traditional plot scheme aswell. p.s. used barplot(frequency, names.arg = c(b)), because read here, there no equivalent in r matlab's bar function.

sometimes improve tell trying do, rather how trying it. looks of r code boxplot scaled histogram , other r code , guesses matlab code want add together reference lines normal distributions. if right going long way in r. next r code much simpler:

x <- rnorm(1000) hist(x, prob=true) curve(dnorm(x,0,1), add=true) curve(dnorm(x,2,2), add=true)

even improve add together col='blue' or similar curve calls. if sense need take own x values can replace calls curve with:

lines(range, dnorm(range, 0, 1) ) lines(range, dnorm(range, 2, 2) )

if want larn add together lines barplot should realize default locations bars may not expect. @ updateusr function in teachingdemos bundle r examples of adding lines barplot.

r matlab plot ggplot2

No comments:

Post a Comment