Pie chart main title with same vertical position as normal plot - r

I want to plot a normal plot and a pie chart (plotrix package) side-by-side via par(mfrow = c(1, 2). The main titles of both graphics should have the same vertical position. However, by default both main titles have a different positioning.
Question: How could I ensure that the main title of the pie chart has the same vertical position as the title in a normal plot?
Consider the following reproducible example in R. "Lower main title" should be at the same height as "Main title with usual height".
# Set panel layout
par(mfrow = c(1, 2))
# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")
# Load plotrix package for piecharts
library("plotrix")
# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "Lower main title")

The problem is that the two panels get different plot regions. If you use pie3D(..., pty = "m") they will get the same plot regions, but the pie will look distorted, unless you've chosen a window that makes the plot region for the pie approximately square.
Another solution is to change the plot region for the pie to match the other one, and plot the title after that. For example,
# Set panel layout
par(mfrow = c(1, 2))
# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")
# Save the plot region
plt <- par("plt")
# Load plotrix package for piecharts
library("plotrix")
# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "")
# Restore the original plot region and add the title
par(plt = plt)
title(main = "Pie title with matching height")
This works until you change the shape of the plot window; the pie region tries to stay square, and it will move the title up or down.

If you don't mind that both plots are the same size you could do this by using layout() function that divides the device according to rows or columns specified.
So first you specify the two rows where your plots will be
# So create matrix with 1 row, and specify size and width of your plots
two.rows.plot <- layout(matrix(c(1, 2), nrow = 1), widths = c(5, 5), heights = c(5, 5), TRUE)
layout.show(two.rows.plot) # how the device is being split up into different figure regions
UPDATE
According to #user2554330 the above code can be replaced with only setting
par(pty = "s")
which means that we set a graphical parameter which will generate a square plotting region.
Then Use your code
# Normal plot with normal height of main
plot(1:10, 1:10, main = "Main title with usual height")
# Load plotrix package for piecharts
library("plotrix")
# Pie chart with lower main title position
pie3D(1:5, labels = letters[1:5], main = "Lower main title")
ant the output:

Related

How to make y axis labels horizontal but keep y-axis title parallel?

I would like to make my y axis labels horizontal, while keeping my y axis titles as parallel.
When I try inputting las=1 into the twoor.plot()argument, nothing happens. I have also tried ylas=1, y_las=1, lylas=1, rylas=1, and nothing happens. The only way I've been able to make my yaxis labels horizontal, is by using par(las=1), but then this makes my y-axis titles horizontal too, which I don't want...
This is my code so far:
par(las=1)
yFrequency <- c(0,20,40,60,80,100,120,140,160)
GS_class_labels <- c("<2", "2-4", "4-8", "8-16", "16-32", "32-64", "64-128", "128<")
twoord.plot(data=distribution,lx="Var1",ly="Freq", ry="cum_percentile",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="Frequency",ylab.at=NA,
rytickpos=NA,rylab="Percent Finer Than (%)",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="s", yticklab=yFrequency, cex.lab=1)
An alternative way to set the y axis labels parallel is as follows.
(1) Set both of the ylab and rylab from twoord.plot to empty.
(2) Use mtext and set the parameters accordingly.
Here is the code to do that. Because you don't provide the distribution data, I use iris data just to make it possible to generate the plot.
# Emptying both of ylab and rylab
twoord.plot(data = iris,lx="Sepal.Length",ly="Petal.Width", ry="Sepal.Width",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="",ylab.at=NA,
rytickpos=NA,rylab="",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="n",yaxt="n", #yticklab=yFrequency,
cex.lab=1)
# Assign the previous labels of ylab and rylab to the *text* parameter of *mtext*.
# side = 2 means the left side. side = 4 means the right side.
# las = 0 is the parallel style of the text.
# line shows the distance of the text from the y axis.
mtext(text = "Frequency", side = 2, las = 0, line = 2.5)
mtext(text = "Percent Finer Than (%)", side = 4, las = 0, line = 0.5)
The resulted plot:

How to adjust the space between 2 columns in a plot legend?

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

Legends overlapping the plots in Rstudio

I am trying to plot an image and then overlay a legend on its top. The legend is covering the plot and I am not able to adjust it using different parameters like cex, lty, etc.
plot(cov16_2ms04h$unqC_Sp, cov16_2ms04h$unqC_My, log="xy",
col=(cov16_2ms04h$binom_q<0.001)+1,
ylab="Haplotype B Count", xlab="Haplotype A Count")
abline(0,1,col="grey")
legend("topleft",c("No significant imbalance","Significant imbalance"),pch=c(10,10),col=c(1,2), cex = 0.5)
is giving me plot as:
But, I want something like:
Thanks,
have you tried enlarge the size of the plot before overlying a legend? I tried you legend code, the legend was perfect on the plot.
The problem is with R-studio because the size of the output plot is determined by the costumized size of the pane.
To plot the results directly to a pdf or a png file the following can be done.
# plotting directly on pdf or png - select the required one
pdf("my_plot.pdf", height=6, width=6)
png("my_plot.png", width = 4, height = 4,
units = 'in', res = 300)
plot(cov16_2ms04h$unqC_Sp, cov16_2ms04h$unqC_My, log="xy",
col=(cov16_2ms04h$binom_q<0.001)+1,
ylab="Haplotype B Count", xlab="Haplotype A Count")
abline(0,1,col="grey") # draw abline
legend("topleft",c("No significant imbalance","Significant imbalance"),
pch=c(1,1),col=c(1,2), cex = 0.75) # add legend
dev.off() # close the plot
The output then comes as:
Thanks,

R lattice multiple plot page how to put text in page margin

I would like to put a title on a page of plots created using R lattice. For example I can put four plots on a page as follows:
#load lattice
require(lattice).
# data
a<-c(1,3,4)
b<-c(1,2,3)
# make plots
plt1<-xyplot(a~b,main="plt1")
plt2<-xyplot(a~b,main="plt2")
plt3<-xyplot(a~b,main="plt3")
plt4<-xyplot(a~b,main="plt4")
# plot plots
plot(plt1, split=c(1,1,2,2),newpage=FALSE)
plot(plt2, split=c(1,2,2,2),newpage=FALSE)
plot(plt3, split=c(2,1,2,2),newpage=FALSE)
plot(plt4, split=c(2,2,2,2),newpage=FALSE)
Now how do a put the title "My Page of Plots" centered in the top margin above plt1 and plt3?
You could use grid to push a viewport and add the title:
library(grid)
vp2 <- viewport(x = 0.5, y = 1, width = 1, height = .1, just = c("center", "top"))
pushViewport(vp2)
grid.rect(gp = gpar(vol = "blue")) # just to see dimensions/position of the viewport
grid.text("My Title", gp = gpar(cex = 2))
You have to play with the position and dimensions of the viewport a bit. And ideally you would also add a top margin to your lattice call, such that you create some white space for your title.

Is there a way to right-justify a main title of a base (hist) plot in R?

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)

Resources