I have 2 plots in a panel via par(mfrow=c(1,2))
op <- par(
oma=c(0,0,3,0),# Room for the title and legend
mfrow=c(1,2)
)
for(i in 1:2) {
plot( cumsum(rnorm(100)), type="l", lwd=3,
col=c("navy","orange")[ 1+i%%2 ],
las=1, ylab="Value",
main=paste("Random data", i) )
}
I would like to plot main title "Random plots" which will fit nicely in the middle of the 2 plots.
I tried the solution from here R - Common title and legend for combined plots using mtext and a lot of experimenting but the mtext solution does not get the title exactly in the middle, just like the main argument of plot().
Is there any alternative?
Related
I want to have the plot legend for a pie chart as a single plot (6 graphics at a page - 5 plots, 1 legend). Now I have difficulties adjusting the space between the 2 columns.
I used the following code (by try and error):
#Colors
colors=c("blue","green","yellow","orange","red","purple","pink","grey")
legtext <- c("G","G*E","E","Source","Source*E",
"Table*E","Table*Block*E","Residual Error")
#Code for chart
pie3D(#data for pie
rooting1,
#specify labels vector
#labels=labels,
#specify labels size
labelcex=0.9,
#how much different pies go from each other
explode=0.1,
#height of chart
height=0.1,
#Main title
theta=pi/3,
#Colors
col=colors
)
#Code for legend
xcoords <- c(0.9,1,1.1,1.2)
secondvector <- (1:length(legtext))-1
textwidths <- xcoords/secondvector # this works for all but the first element
textwidths[1] <- 0
legend(-1, 0.9,ncol=2,
c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"),
cex = 0.8,
fill = colors,
text.width=textwidths)
The plot I get is this: I want to remove the vertical lines and if possible, remove the rest of the chart as I only want to display the legend.
Legend I get:
Can anybody help me?
Add bty="n" to your legend:
legend(-1, 0.9,ncol=2,
c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"),
cex = 0.8,
fill = colors,
text.width=textwidths,
bty="n")
As for the other question - how to get rid of the chart itself, this will take some fiddling. Basically, what you can do is to make an empty chart, but adjusting the xlim and ylim, as well as margins so that there is enough room for the legend:
par(mar=c(0.1,0.1,0.1,0.1)) # you don't need large margins
# but maybe you need more than 0.1
plot(NA, xlim=c(-1,1), ylim=c(-1,1), axes=FALSE, xlab="", ylab="")
# this makes an empty plot
# you may need to change xlim and ylim (or the x and y of your legend)
# ... so that the legend would start from the left/upper corner
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'm trying to get the title of a plot to align to the right-most part of the plot.
To clarify, in the example to follow, the title of the plot is aligned to the center.
op <- par(mfrow=c(2, 2))
hist(islands)
utils::str(hist(islands, col="gray", labels = TRUE))
Use separate function for the main title and graphical parameter adj:
op <- par(mfrow=c(2, 2))
hist(islands,main=NULL)
title("Histogram of islands",adj=1)
utils::str(hist(islands, col="gray", labels = TRUE,main=NULL))
title("Histogram of islands",adj=1)
I would like to know how to provide a common title and legend for combined plots in R. I have four plots which I have combined into one. Each plot has its own Title. I want to specify a common Title on the top center and common legend on the top left corner of the combined plot. I generated the combined plot using par(). I have provided my plot below
You can use the oma parameter to increase the outer margins,
then add the main title with mtext,
and try to position the legend by hand.
op <- par(
oma=c(0,0,3,0),# Room for the title and legend
mfrow=c(2,2)
)
for(i in 1:4) {
plot( cumsum(rnorm(100)), type="l", lwd=3,
col=c("navy","orange")[ 1+i%%2 ],
las=1, ylab="Value",
main=paste("Random data", i) )
}
par(op) # Leave the last plot
mtext("Main title", line=2, font=2, cex=1.2)
op <- par(usr=c(0,1,0,1), # Reset the coordinates
xpd=NA) # Allow plotting outside the plot region
legend(-.1,1.15, # Find suitable coordinates by trial and error
c("one", "two"), lty=1, lwd=3, col=c("navy", "orange"), box.col=NA)
I have an ultra short question about R
My aim is to assign a common title to a multi-panel plot generated using par, e.g.
par(mfrow=c(1,2))
plot(rnorm(1000))
plot(rnorm(1000))
So, something like "main" for the plot function, but extended to both plots. Is there a canonical way to do this?
Thanks for any answer :-)
Use mtext with option outer:
set.seed(42)
oldpar <- par(mfrow=c(1,2), mar=c(3,3,1,1), oma=c(0,0,3,1)) ## oma creates space
plot(cumsum(rnorm(100)), type='l', main="Plot A")
plot(cumsum(rnorm(100)), type='l', main="Plot B")
mtext("Now isn't this random", side=3, line=1, outer=TRUE, cex=2, font=2)
par(oldpar)