Combine integrated R plots with ggplot - r

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!!

Related

Two histograms on one one plot without overlap

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.

R Barplot: Y-axis cut off at the top?

I'm trying to use R to do a barplot. Values I'm plotting range from 0 to 5.0, but are decimal values (such as 4.87) so I don't want to just use the default Y axis, because it just goes up in increments of 1.
I've created a custom Y axis, which works, but if I set the maximum value greater than about 4.5, it cuts off the tickmark at the top of the axis. This looks untidy so I want a way to ensure this tickmark will always appear, but I don't want to shorten my axis as it looks stupid if I do this.
My R code is as follows:
# Bar plot of mean SUS question scores
barplot(meanSUSQuestions$Mean,
main="Mean SUS Question Scores",
cex.main="0.8",
cex.axis="0.8",
cex.lab="0.8",
#names=c("q1", "q2", "q3","q4","q5","q6","q7","q8","q9","q10"),
names=c(1:10),
yaxt="n",
col="red")
axis(2, cex.axis="0.8", at=seq(0, 5, 0.5)) # Create custom Y axis
mtext(text="Mean Score", side=2, line=2, cex=0.8)
mtext(text="Question", side=1, line=2, cex=0.8)
The bar plot that this produces looks like this:
As you can see from the picture, the top tickmark is missing.
How can I get this top tickmark to appear?
barplot generates the image height based on the data. The range of your manual y-axis is considerably larger than the plot area and is thus cut off.
The easiest way to solve the issue in your specific case is to add an yaxp = c(0, 5, 11) to barplot instead of yaxt = "n" and axis.
A self-contained example:
# Bad
x <- 1:5
barplot(x, yaxt = "n") #, add = TRUE)
axis(2, at = seq(0, 6, 2)) # Create custom Y axis
# Good
barplot(x, yaxp = c(0, 6, 2))

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

Plot average of values in single plot

I want to plot single bar in a graph so it would look like picture below
I created test data and calculate its mean.
value <- c(99,44,100,120,88)
value_mean <- mean(value)
And plot them using below code
barplot(value_mean, horiz=T, width=30, cex.names=0.5 ,ylim=c(0,200), col="red")
Buth the output is not even close.
I've also looked at this links
Single bar barchart in ggplot2, R
R Barplot with one bar - how to plot correctly
So my output should be something like the first picture. I was thinking that could solve ggplot.
If everything else fails, you can draw a rectangle like this:
par(mar = c(12, 2, 12, 2))
plot(0, type="n", ylim=c(-1, 1), xlim=c(0, 200), axes = F, bty = "n",ylab="", xlab="label")
rect(0, -.7, value_mean, .7, col="red", border=NA)
text(value_mean, 0, pos=4, label=value_mean)
axis(1, at=seq(0, 200, by=40))

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

Resources