More plots in an unique page: the titles are hidden - r

I'm trying to plot 6x4 graphs on a page, each with its title. I have used a lot of options.
pdf("myDocument.pdf") #, width = 50, height =60)
par(mfrow=c(6,4), oma = c(2,0,2,0) + 0.0)
par(mar=c(2, 0, 1, 0) + 0.1)
par(cex.axis=0.1)
par( mgp = c(1, 1, 0),
omd = c(1.0, 0, 1.0, 0),
pin=c(10,10),
plt = c(1,1,1,1),
usr =c(1,0,1,0)
)
for(z in 1:ncol(df_finale)){
...
corrplot(matriceTermine,
title = colnames(df_finale)[z],
xaxs = "i",
#insig = "pch",
#pch.col="blue",
#addrect=24, rect.lwd=10,
#addCoefasPercent=T,
method = "shade", # shade ellipse
order="original",
col=col1(11),
addgrid.col="grey",
addCoef.col="grey",
mar = c(0,0,0,0),
cl.cex= 0.2, cl.pos = "n", # 0.2
tl.pos = "n", tl.cex= 0.3, tl.offset = 0.9, tl.srt = 90,
#tl.pos="d")
}
dev.off()
It results:
Which parameters do I have to modify to make the titles visible?
And suppose I want to reduce the dimensions of the plots (to give extra-spaces to the titles), how can I do?

I have solved in this way, adding:
...
corrplot(matriceTermine,
#title = colnames(df_finale)[z],
...,
method = "s")
title(colnames(df_finale[z]), line = -0.1, cex = 2.5, font=3, cex.main = 2)
...
With
line = - q
it's possible to "move" the title of q.
For successive modifications I suggest to save the plot in a "svg" and modifying it with Inkscape.

Related

For-loop to dynamically change plot title

I have 5 variables which want to plot and export in one pdf. However, I have some trouble wiht the for-loop I am running,
parC <-list(unit = 100,labelx = "Time",labely = "Time",cols = "black",
pcex = .01, pch = 1,las = 1,
labax = seq(0,nrow(RP),100),
labay = seq(0,nrow(RP),100))
pdf("filename.pdf", onefile=TRUE)
for (i in RP_values){ # the values that are plotted
for (j in name) { # name is a list of names, so that the title changes dynamically
plotting(i, parC, j)
}
}
dev.off()
RP_values = list of values that is plotted
name = list of names to dynamically change the plotting title
plotting = an adjusted version from the plotRP() function of the crqa package. Here I added a main title to the plot.
The code for the plotting() function:
plotting <- function(RP, par, x){
if (exists("par") == FALSE){ # we use some defaults
## default values
unit = 2; labelx = "Time"; labely = "Time"
cols = "black"; pcex = .3; pch = 1; las = 0;
labax = seq(0, nrow(RP), unit); labay = seq(0, nrow(RP), unit);
} else { # we load the values that we desire
for (v in 1:length(par)) assign(names(par)[v], par[[v]])
}
xdim = nrow(RP)
ydim = ncol(RP)
RP = matrix(as.numeric(RP), nrow = xdim, ncol = ydim) # transform it for plotting
ind = which(RP == 1, arr.ind = T)
tstamp = seq(0, xdim, unit)
par(mar = c(5,5, 1, 3), font.axis = 2, cex.axis = 1,
font.lab = 2, cex.lab = 1.2)
plot(tstamp, tstamp, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = x)
matpoints(ind[,1], ind[,2], cex = pcex, col = cols, pch = pch)
mtext(labelx, at = mean(tstamp), side = 1, line = 2.2, cex = 1.2, font = 2)
mtext(labely, at = mean(tstamp), side = 2, line = 2.2, cex = 1.2, font = 2)
# if (is.numeric(labax)){ ## it means there is some default
# mtext(labax, at = seq(1, nrow(RP), nrow(RP)/10), side = 1, line = .5, cex = 1, font = 2)
# mtext(labay, at = seq(1, nrow(RP), nrow(RP)/10), side = 2, line = .5, cex = 1, font = 2)
# } else{
mtext(labax, at = tstamp, side = 1, line = .5, cex = .8, font = 2, las = las)
mtext(labay, at = tstamp, side = 2, line = .5, cex = .8, font = 2, las = las)
# }
}
My problem is instead of 5 plots I get 25, where each plot appears 5 times, but with a different title. If I do not include the "j" part everything works fine, but of course do not have any main title for each plot.
I appreciate any help.
Best,
Johnson
From your description and comments, it appears you need an elementwise loop and not a nested loop. Consider retrieving all pairwise combinations of names and RP_values with expand.grid and iterate through them with mapply. Also, since parC depends on nrows of corresponding RP, have parC defined inside function for only two parameters (with more informative names like title instead of x):
plotting <- function(RP, title) {
parC <- list(unit=100, labelx="Time", labely="Time",
cols="black", pcex=.01, pch=1, las=1,
labax=seq(0, nrow(RP), 100),
labay=seq(0, nrow(RP), 100))
...
plot(tstamp, tstamp, type="n", xlab="", ylab="",
xaxt="n", yaxt="n", main=title)
...
}
params <- expand.grid(RP_values=RP_values, name=name)
out <- mapply(plotting, RP=params$RP_values, title=params$name)

Plotting over multiple lengths and columns

I am wanting to plot on the X axis 17K sets of intervals, where I plot the start and stop intervals for each Chr column. However, these intervals are not plotting correctly to each chr? This I figured out from how the plot was running off the right side and the red dots did not match up with input data. Thoughts on a fix? To be clear chr.len is the length of each chromosome.
Data;
ID Chr Start Stop
XLOC_007681 2R 11896162 11896597
XLOC_024365 3R 11283380 11286479
XLOC_021494 3R 16392979 16396291
XLOC_012125 3L 136830 138533
XLOC_031405 X 8002493 8004054
XLOC_014371 3L 15537489 15538755
XLOC_005808 2L 20704834 20706685
XLOC_005809 2L 20706861 20708183
XLOC_005807 2L 20703325 20703897
============================================================
chr.len <- c(22422827, 204112, 347038, 23011544, 368872, 21146708, 3288731, 24543557, 2555491, 27905053, 2517507, 1351857, 10049037)
names(chr.len) <- c("X", "XHet", "YHet", "2L", "2LHet", "2R", "2RHet", "3L", "3LHet", "3R", "3RHet", "4", "U")
chr.gap <- 2000000
chr.cum <- cumsum(c(0, chr.len[1:12])) + (0:12)*chr.gap
names(chr.cum) <- names(chr.len)
# ============================================================
png(file = "C:/Users/cahighfi/Desktop/XLOC_Position.png", width = 10, height = 5, units = "in", res = 300)
plot(c(0, chr.cum["U"] + chr.len["U"]), c(0, 1), type = "n", axes = FALSE, ylab = "", xlab = "", )
segments(XLOC.pos$Start + chr.cum[XLOC.pos$Chr], 0.5, XLOC.pos$Stop + chr.cum[XLOC.pos$Chr], 0.5, lwd = 10)
segments(DrugXLOC.pos$Start + chr.cum[DrugXLOC.pos$Chr], 0.5, DrugXLOC.pos$Stop + chr.cum[DrugXLOC.pos$Chr], 0.5, lwd = 10, col = c("red"))
axis(side = 1, at = chr.cum + chr.len/2, labels = parse(text = paste("italic(\"", names(chr.len), "\")", sep = "")), mgp = c(2.5, 0.5, 0), tck = -0.015, cex.axis = 1.0)
dev.off()
[Output plot][[1]]
[[1]]: https://i.stack.imgur.com/qPObX.png

How can I code a self arrow in my node diagram in R?

The code I found creates a population matrix node diagram. All I want to do is add a self arrow on the final node i.e. one that points to itself. Hope you can help.
library(diagram)
Numgenerations <- 6
DiffMat <- matrix(data = 0, nrow = Numgenerations, ncol = Numgenerations)
AA <- as.data.frame(DiffMat)
AA[[1,5]] <- "f[4]"
AA[[1,6]] <- "f[5]"
#
AA[[2,1]] <- "s[list(0,1)]"
AA[[3,2]] <- "s[list(1,2)]"
AA[[4,3]] <- "s[list(2,3)]"
AA[[5,4]] <- "s[list(3,4)]"
AA[[6,5]] <- "s[list(4,5)]"
#
name <- c(expression(Age[0]), expression(Age[1]), expression(Age[2]),
expression(Age[3]), expression(Age[4]), expression(Age[5]))
#
plotmat(A = AA, pos = 6, curve = 0.7, name = name, lwd = 2,
arr.len = 0.6, arr.width = 0.25, my = -0.2,
box.size = 0.05, arr.type = "triangle", dtext = 0.95,
main = "Age-structured population model")
You can add the arrow this way:
AA[[6,6]] <- "s[list(5,5)]"
Or, if you want it labeled as a self arrow,
AA[[6,6]] <- "self"
I needed to set relsize to slightly less than 1 to prevent the self arrow from being clipped at the right edge.
plotmat(A = AA, pos = 6, curve = 0.7, name = name, lwd = 2,
arr.len = 0.6, arr.width = 0.25, my = -0.2,
box.size = 0.05, arr.type = "triangle", dtext = 0.95,
main = "Age-structured population model",
relsize=0.97)

How to set the y range in boxplot graph?

I'm using boxplot() in R. My code is:
#rm(list=ls())
#B2
fps_error <- c(0.058404273, 0.028957446, 0.026276044, 0.07084294, 0.078438563, 0.024000178, 0.120678965, 0.081774358, 0.025644741, 0.02931614)
fps_error = fps_error *100
fps_qp_error <-c(1.833333333, 1.69047619, 1.666666667, 3.095238095, 2.738095238, 1.714285714, 3.634146341, 5.142857143, 1.238095238, 2.30952381)
bit_error <- c(0.141691737, 0.136173785, 0.073808209, 0.025057931, 0.165722097, 0.004276999, 0.365353752, 0.164757488, 0.003362543, 0.022423845)
bit_error = bit_error *100
bit_qp_error <-c(0.666666667, 0.785714286, 0.428571429, 0.142857143, 0.785714286, 0.023809524, 1.523809524, 0.976190476, 0.023809524, 0.142857143)
ssim_error <-c(0.01193773, 0.015151569, 0.003144532, 0.003182908, 0.008125274, 0.013796366, 0.00359078, 0.019002591, 0.005031524, 0.004370175)
ssim_error = ssim_error * 100
ssim_qp_error <-c(3.833333333, 1.80952381, 0.69047619, 0.571428571, 2, 1.904761905, 0.761904762, 2.119047619, 0.857142857, 0.976190476)
all_errors = cbind(fps_error, bit_error, ssim_error)
all_qp_errors = cbind(fps_qp_error, bit_qp_error, ssim_qp_error)
modes = cbind(rep("FPS error",10), rep("Bitrate error",10), rep("SSIM error",10))
journal_linear_data <-data.frame(fps_error, fps_qp_error,bit_error,bit_qp_error,ssim_error,ssim_qp_error )
yvars <- c("fps_error","bit_error","ssim_error")
yvars_qp <-c("fps_qp_error","bit_qp_error","ssim_qp_error")
xvars <- c("FPS", "Bitrate", "SSIM")
graphics.off()
bmp(filename="boxplot_B2_error.bmp")
op <- par(mfrow = c(1, 3), #matrix of plots
oma = c(0,0,2,0),mar=c(5.1, 7.1, 2.1, 2.1),mgp=c(4,1,0)) #outer margins
par(cex.lab=3)
par(cex.axis=3)
for (i in 1:3) {boxplot(journal_linear_data[,yvars[i]], xlab=xvars[i], ylab="Percentage error", outcex = 2)}
par(op)
mtext(text="Percentage error per mode for B2",side=3, line=1.5, font=2, cex=2,adj=0.95, col='black')
dev.off()
The image output is shown below. As you can see the y-axis does not have the same range for all graphs. How can I fix this? For example starting in 0.5 or 0.
You can simply put an ylim = c(0, 5) in all your boxplot() call. This sets y-axis range (roughly) between 0 and 5.
Perhaps you did not see ylim argument in ?boxplot; the "Arguments" section also does not mention it. But ylim is just a trivial graphical parameter passed via "...". You can also find such example in the "Examples" session of ?boxplot:
boxplot(len ~ dose, data = ToothGrowth,
boxwex = 0.25, at = 1:3 - 0.2,
subset = supp == "VC", col = "yellow",
main = "Guinea Pigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length",
xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")

R: How to add highlighted angle lines in polar plots?

Please consider the following sample polar plot:
library(plotrix)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0, 350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
I would like to add lines at angles 30 and 330 as well as 150 and 210 (from the center to the outside). I experimented with the line function but could not get it to work.
The calculations for exact placement are a bit goofy but using your test data
set.seed(15)
testlen<-c(rnorm(36)*2+5)
testpos<-seq(0,350,by=10)
polar.plot(testlen,testpos,main="Test Polar Plot",
lwd=3,line.col=4,rp.type="s")
You can add lines at 20,150,210,300 with
add.line <- c(30,330, 150,210)/360*2*pi
maxlength <- max(pretty(range(testlen)))-min(testlen)
segments(0, 0, cos(add.line) * maxlength, sin(add.line) * maxlength,
col = "red")
And that makes the following plot
You can just use the rp.type = "r" argument and add = TRUE. So, something like
library(plotrix)
set.seed(1)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0,350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
followed by
pos <- c(30, 330, 150, 210)
len <- c(10, 10, 10, 10)
polar.plot(lengths = len, polar.pos = pos,
radial.lim = c(0, 15),
lwd = 2, line.col = 2, rp.type = "r", add = TRUE)
yields your desired output.

Resources