0
votes

I am trying to plot two sets of data on one histogram, but I dont want the bars to overlap, just to be next to each other in the same plot. currently I am using the code:

plot(baxishist1,freq=FALSE, xlab = 'B-Axis (mm)', ylab = 'Percent of Sample', main = 'Distribution of B-Axis on Moraine 1', ylim=c(0,30),breaks=seq(25,60,1), col='blue')

par(new=T)

plot(baxishist2,freq=FALSE, xlab = 'B-Axis (mm)', ylab = 'Percent of Sample', main = 'Distribution of B-Axis on Moraine 2', ylim=c(0,30),breaks=seq(25,60,1), col='red')

and the results are bars overlapping on histogram

Can anyone help me to make the bars to be in the same bins but not overlap so that I can see both histograms?

1
Hello Jane, it would be a great help if you could reformat your question and provide a basic sample of data to help us help you better. You can find every tips for this purpose here : how-to-make-a-great-r-reproducible-example.cbo
I would recommend not to keep the two histrograms in the same plot - no matter what visual tricks you use it will always be hard to read. I would plot two separate histograms forcing identical x-axes and show them in a column - much easier to interpret/compare.Phil

1 Answers

0
votes

You can make this a little easier to interpret, by using transparent colors.

Let's fist generate some data:

a <- rnorm(100)
b <- rnorm(100, mean=3)

And now plot the histograms:

hist(a, col=rgb(1,0,0,0.5))
hist(b, col=rgb(0,1,0,0.5), add=T)

enter image description here

As you can see, both are now somewhat visible but we would now have to manually adjust the x-axis to accomodate both distributions. And in any case, it's still not nice to read/interpret so I would rather plot two separate histograms, a boxplot or a violinplot.