1

I would like to draw a histogram to compare two datasets on the same plot. The length of the vectors are different. d1 has 180 numbers and d2 has 300 numbers.I tried the following code as example.

d1<-c(5.18,4.61,3.30,7.58,3.00,3.80,1.95,2.67,2.77,2.73,2.33,3.36,3.50,1.91,4.25,3.87,2.86,2.26,2.00,3.86,3.33,3.59,4.00,2.25,2.44,2.33,3.22,4.42)
d2<-c(2.1,2.1,2.6,2,3.6,2,2.7,1.8,3.1,3.9,3.8,2.6,1.9,2.6,2.1,3.9)
names(d1)<-1:28
names(d2)<-1:15
barplot(t(cbind(d1, d2)), beside=T, col=c("black","red"), border=F)
barplot(t(cbind(d1/sum(d1)*100, d2/sum(d2)*100)), 
    beside=T, col=c("black","red"), border=F)

I don't know whether this method is correct or not. x axis should be as bins. y axis should be in frequency percentages. your suggestions would be appreciated!!

For example I would like to get the plot as shown below

enter image description here

ayya
  • 31
  • 1
  • 4

1 Answers1

0

You can add to the second "barplot" function the argument "add=T":

barplot(t(cbind(d1/sum(d1)*100, d2/sum(d2)*100)), beside=T, col=c("black","red"), border=F, add=T)

which will make the two histograms be on the same figure, and even share the same x-axis.

Slava Bronfman
  • 160
  • 1
  • 6