Two histograms on one one plot without overlap - r

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?

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)
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.

Related

Combine integrated R plots with ggplot

I am trying to make a layout plot that merges 3 plots. I want the first plot in the first row and the other ones in the second row like this image:
The problem comes when I try to combine plots integrated in R and a plot made with ggplot2 library.
My code is the following:
layout(matrix(c(1, 1,
2, 3), nrow=2, byrow=TRUE))
layout.show(n=3)
qplot(area_mean,
geom="histogram",
binwidth = 5,
main = "ggplot area min histogram",
xlab = "Age",
fill=I("blue"),
col=I("red"),
alpha=I(.2),
xlim=c(0,3000))
hist(area_mean, col="purple", main="Histogram of area mean")
boxplot(area_mean, col="cyan", main="Boxplot of area mean")
Then my output image is:
Why qplot doesn't appear? I am missing something?
Thank you!!

Overlapping histograms on one page in R

I am a beginner with R. I managed to plot my data into overlapping histograms. However, I would like to place all the histograms on one page. I am struggling as I am not able to tell R, which sets to pick (only manage to plot one of the plots).
This is the code:
df<-read.csv("Salt dshalo sizes.csv",header=T)
#View(df)
library(ggplot2)
DSA<-df[,1]
DS1<-df[,2]
DSB<-df[,5]
DS2<-df[,6]
DSC<-df[,9]
DS3<-df[,10]
#remove the NA column by columns separately or it will chop the data
DSA=na.omit(DSA)
DS1=na.omit(DS1)
DSB=na.omit(DSB)
DS2=na.omit(DS2)
DSC=na.omit(DSC)
DS3=na.omit(DS3)
#plot histograms for DSA, DSB and DSC on one same graph
hist(DSA, prob=TRUE, main="Controls", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DSB, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DSC, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("Control 1", "Control2", "Control3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
#plot histograms for DS1, DS2 and DS3 on one same graph
hist(DS1, prob=TRUE, main="Monoculture Stressed", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DS2, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DS3, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("DS1", "DS2", "DS3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
# put both overlapping histograms onto one page
combined <- par(mfrow=c(1, 2))
plot(hist(DSA),main="Controls")
plot(hist(DS1),main="Monoculture stressed")
par(combined)
Basically, I get two separate overlapping histograms, but cannot put them on the same page.
EDIT: I evidently didn't read your question thoroughly. I see you figured out the add =T.
I assume what you are looking for then is the comment I made first:
par(mfrow = c(a,b)) where a and b are the number of rows and columns you want the graphics objects to be printed. I used c(2,2) for this pic.
I made a comment, but sounds like you may be talking about the add=T option.
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col="yellow")
hist(b, add=T, col="purple" )
you can play around with transparency options on colors to see both overlap. Such as rgb(1,0,0,1/4) as the color.
With transparency colors:
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col=rgb(1,1,0,1/4))
hist(b, add=T, col=rgb(1,0,0,1/4) )

I am trying to label a grouped-bar chart in R and cannot seem to get the coding down

I am trying to make a grouped bar chart. Right now this is what I have:
Grouped Bar Chart
For background: I have quite a large data set with multiple variables. What I am interested in for this bar chart is visually representing the median inspection distance (cm) that male guppies (yes, fish) will inspect a predator in the presence and absence of females. As you can see, below the two bar charts there is "A" and "B".... I want these to say "Bright" and "Drab"... I cannot seem to get anything to work!!
this is my code right now:
barplot(matrix(c(18.41,7.20,21.40,11.17),nr=2), beside=T,
col=c("aquamarine3","snow3"), ylim=c(0, 25),
names.arg=LETTERS[1:2], xlab = "Colour", ylab = "Inspection Frequency (cm)")
legend("topright", c("Present","Absent"), pch=15, col=c("aquamarine3","snow3"),
bty="n")
thank you in advance - I know this is a super basic question but I am fairly new at this!
You can suppress the plotting of the x axis labels and then add labels of your own. To see where barplot draws each of the bars, save it to an object.
myplot <- barplot(matrix(c(18.41,7.20,21.40,11.17),nr=2),
beside=T, xaxt="n",
col=c("aquamarine3","snow3"), ylim=c(0, 25),
names.arg=LETTERS[1:2], xlab = "Colour",
ylab = "Inspection Frequency (cm)")
axis( 1, at=colMeans(myplot), labels=c("Bright","Drab"))

histogram for multiple variables in R

I want to make a histogram for multiple variables.
I used the following code :
set.seed(2)
dataOne <- runif(10)
dataTwo <- runif(10)
dataThree <- runif(10)
one <- hist(dataOne, plot=FALSE)
two <- hist(dataTwo, plot=FALSE)
three <- hist(dataThree, plot=FALSE)
plot(one, xlab="Beta Values", ylab="Frequency",
labels=TRUE, col="blue", xlim=c(0,1))
plot(two, col='green', add=TRUE)
plot(three, col='red', add=TRUE)
But the problem is that they cover each other, as shown below.
I just want them to be added to each other (showing the bars over each other) i.e. not overlapping/ not covering each other.
How can I do this ?
Try replacing your last three lines by:
plot(One, xlab = "Beta Values", ylab = "Frequency", col = "blue")
points(Two, col = 'green')
points(Three, col = 'red')
The first time you need to call plot. But the next time you call plot it will start a new plot which means you lose the first data. Instead you want to add more data to it either with scatter chart using points, or with a line chart using lines.
It's not quite clear what you are looking for here.
One approach is to place the plots in separate plotting spaces:
par("mfcol"=c(3, 1))
hist(dataOne, col="blue")
hist(dataTwo, col="green")
hist(dataThree, col="red")
par("mfcol"=c(1, 1))
Is this what you're after?

How to place reference lines ['abline()'] in background?

This is perhaps very basic, but I just can't seem to find a working solution:
I'm building a boxplot with custom axes using the 'boxplot()' function in R, and I'd like to have thin grey lines for reference across the y-tick intervals, something like:
boxplot("MyDataTable", ylim=ylim, axes=FALSE, col=312, notch=TRUE)
axis(2, lwd=1.5, at=ytk, las=2, tck=-0.02, cex.axis=0.75, font=2)
abline(h=yln, lty=1.5, lwd=0.5, col=336)
When this prints out (to pdf, in my case), the thin grey lines overlap the boxplot's boxes and whiskers.
How can I have the same plot with the graphed boxes and whiskers being in the foreground...?
One way is just to repeat the boxplot call by adding it to the existing plot, so the horizontal lines become background.
For example:
boxplot(count ~ spray, data = InsectSprays, col = "lightgray", main = "plot title")
abline(h = 1:25, lty=1.5, lwd=0.5, col=336)
boxplot(count ~ spray, data = InsectSprays, col = "lightgray", add = TRUE)
Since you also need interaction with the axis ticks, you might find something similar works there as well but your code is not reproducible, so we can only guess at the actual effect you want to see.
Simple boxplot, overplotted on horizontal lines http://beta1.opencpu.org/R/call/store:tmp/a2884b758f76d5c808e0f9751c35ad74/png?main=%22plot%20title%22

Resources