2

I would like your help, please.

I have this 2 plots, separately. One is normal frequency and the other one, with exactly the same data, is for relative frequency.

Can you tell me how can i join them in a single plot with 2 y axis ( frequency and relative frequency?)

x<- AAA$starch 
h<-hist(x, breaks=40, col="lightblue", xlab="Starch ~ Corn", 
    main="Histogram with Normal Curve", xlim=c(58,70),ylim = c(0,2500),axes=TRUE)
xfit<-seq(min(x),max(x),length=40) 
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=3)

frequency plot

library(HistogramTools)
x<- AAA$starch 
c <- hist(x,breaks=10, ylab="Relative Frequency", main="Histogram with Normal Curve",ylim=c(0,2500), xlim=c(58,70), axes=TRUE)
PlotRelativeFrequency((c))

relative frequency

Thank you!!

EDIT:

This is just an example image of what I want...

2yaxisplot

Ana Raquel
  • 155
  • 3
  • 13
  • Is this relative frequency just the same data, divided by the total _n_? Are you sure you need a separate plot for that? Maybe just add a second y axis? – juod Feb 28 '17 at 07:55
  • @joud. Yes, i want a single plot with both information in different y axis. – Ana Raquel Feb 28 '17 at 11:10

1 Answers1

1

I use doubleYScale from package latticeExtra.

Here is an example (I am not sure about relative frequency calculation) :

library(latticeExtra)

set.seed(42)
firstSet <- rnorm(500,4)

breaks = 0:10

#Cut data into sections
firstSet.cut = cut(firstSet, breaks, right=FALSE)
firstSet.freq = table(firstSet.cut) 

#Calculate relative frequency
firstSet.relfreq = firstSet.freq / length(firstSet) 

#Parse to a list to use xyplot later and assigning x values
firstSet.list <- list(x = 1:10, y = as.vector(firstSet.relfreq))

#Build histogram and relative frequency curve
hist1 <- histogram(firstSet, breaks = 10, freq = TRUE, col='skyblue', xlab="Starch ~ Corn", ylab="Frequency", main="Histogram with Normal Curve", ylim=c(0,40), xlim=c(0,10), plot=FALSE)
relFreqCurve <- xyplot(y ~ x, firstSet.list, type="l", ylab = "Relative frequency", ylim=c(0,1))

#Build double objects plot
doubleYScale(hist1, relFreqCurve, add.ylab2 = TRUE)

And here is the result with two y axis with different scales :

Frequency histogram and relative frequency curve

chateaur
  • 346
  • 1
  • 13
  • but i don't want two barplots. just want to add the information about the relative frequency. If I use the same number of breaks the plot will be exactly the same, so i would want to see on the left side the frequency and in the right side, the relative frequency. is that possible? – Ana Raquel Feb 28 '17 at 13:59
  • I think I don't understand your issue. Ok, so the first plot is a histogram. The second one is also a histogram right ? Now you have three possibilities : 1. The barplots are mixed (solution above) 2. You add an offset to x values to relative frequency so that the second histogram is on the right 3. You make two plots – chateaur Feb 28 '17 at 14:38
  • Question edited with an image explaining what i am trying to do... Thank you. – Ana Raquel Feb 28 '17 at 15:04
  • @AnaRaquel is it what you are looking for ? – chateaur Mar 06 '17 at 07:23
  • Thank you for your answer but unfortunately this is not what i want. I just want to add a second y axis (relative frequency) but i still want my normal curve, as i showed in my first graph. In the code that you gave me you're creating a line for the normal distribution... What i am asking, is that possible to do? – Ana Raquel Mar 09 '17 at 12:17