levelplot (R lattice package) with 2 yaxis labels - r

I want to place 2 sets of y-axis labels on the graph I created using levelplot function from lattice library at R. I was able to get two sets of labels to show but they are overlapping. Below please see a minimum example. I also tried a few options at par.settings such as ylab.axis.padding and axis.components padding, but nothing seemed to change the superimposition of 2 y-labels. Perhaps they have been overwritten somehow? Any ideas will be appreciated.
My example codes :
A = matrix( c(3, 1, 0, 1, 2, 3, 1, 0,
rep(1,4), 2, 0, 1), nrow=3, ncol=5, byrow = TRUE)
colnames(A)= c("XXX5", "XXX4", "XXX3", "XXX2", "XXX1")
axis.build=function(side,...){
if(side == "left"){
panel.axis(side=side, outside=TRUE, at=1:5,tck=0,
text.col="black", labels=colnames(A), text.cex=0.5)
panel.axis(side=side, outside=TRUE, at=1:5,tck=0,
text.col="brown", labels=seq(ncol(A)), text.cex=0.9 )
} else axis.default(side=side, ...)
}
levelplot(A, aspect="iso", shrink = c(0.8, 0.8),
scales= list(x=list(draw=F),cex=0.5, font=2),
axis=axis.build,, xlab= NULL, ylab=NULL,
col.regions=c("black", "orange", "red","purple"),
at=c(-1, 0, 1, 2, 3), colorkey = FALSE,
par.settings = list(axis.line=list(col="transparent"),
axis.components=list(bottom=list(pad1=1, pad2=3)) ))

I think I have found the solution. In case anyone interested : I changed the first tck=0 to tck=2, and added line.col= "transparent", thus two left axis are stacked next to each other. Voila ! However, I can't seem to find where the documentation is for using pad1 and pad2 parameters. Any suggestion?

Related

label certain points with textxy()

I am trying to plot a volcano plot in R using the plot function and calibrate package in R and am trying to use the textxy function to plot only certain points.
Here is some data:
Metabolites <- data.frame(Metabolite = c("Glucose", "Galactose", "Creatine", "Lactose", "N-Acetylputrescine", "Tyramine", "Adenine", "Glycine", "Erythritol", "Choline"), Neg_pvalue = c(10, 8, 2, 1, 0.5, 0.7, 5, 3, 5.8, 4), LogFC = c(4, -3, 2, -1, 0.5, 0.7, 1, -2, -4, -1), padjust = c(1.453557e-19, 5.312771e-08, 4.983176e-02, 9.585447e-01, 2.449707e-01, 3.058580e-01, 4.223173e-02, 1.002379e-03, 4.466316e-27, 1.003879e-01))
Here is my code:
with(Metabolites, plot(LogFC, Neg_pvalue, pch=20, main="CNL", xlim=c(-5,6)))
with(subset(Metabolites, padjust <.05 ), points(LogFC, Neg_pvalue, pch=20, col="blue"))`
with(subset(Metabolites, padjust <.05 & abs(LogFC) > 2), points(LogFC, Neg_pvalue, ph=20, col="red"))
Now here is the issue:
with(subset(Metabolites, padjust <.05 & abs(LogFC) > 2), textxy(LogFC, Neg_pvalue, labs=Metabolite[1:3], cex=.5, offset = 0.2))`
If I plot this code, I get only the top 3 data points, as is indicated with the labs=Metabolite[1:3] part of the code. Alternatively, if I plot labs=Metabolite, then I get all labels.
If I wanted to plot the labels of only: Glycine, Lactose, and Erythritol as given in the Metabolites$Metabolite, am I able to do this?
Also, say I wanted to keep my top 3 data points labeled (labs=Metabolite[1:3]), but also want to label other metabolites of interest, say Tyramine and N-Acetylputrescine too; how can I do this?
This seems to work by slecting items that are in that set and using those character values as lables:
library(calibrate)
with(subset(Metabolites, Metabolite %in% c( 'Glycine', 'Lactose', 'Erythritol' )),
textxy(LogFC, Neg_pvalue, labs=c( 'Glycine', 'Lactose', 'Erythritol' ), cex=.5, offset = 0.2))

turn off grid lines for R xyplot timeseries

I am plotting a time series with the timePlot function of the open air package of R. The graph has grey grid lines in the background that I would like to turn off but I do not find a way to do it. I would expect something simple such as grid = FALSE, but that is not the case. It appears to be rather complex, requiring the use of extra arguments which are passed to xyplot of the library lattice. I believe the answer lies some where in the par.settings function but all attempts have failed. Does anyone have any suggestions to this issue?
Here is by script:
timeozone <- import(i, date="date", date.format = "%m/%d/%Y", header=TRUE, na.strings="")
ROMO = timePlot(timeozone, pollutant = c("C7", "C9", "C10"), group = TRUE, stack = FALSE,y.relation = "same", date.breaks = 9, lty = c(1,2,3), lwd = c(2, 3, 3), fontsize = 15, cols = c("black", "black"), ylab = "Ozone (ppbv)")
panel = function(x, y) {
panel.grid(h = 0, v = 0)
panel.xyplot(x,y)
}

How to move plot in R

I want to plot a shapefile and add a colorbar beside. I added the coloarbar by the following code.
image.plot(legend.only = TRUE, zlim = c(0, 1000),
col = colorbar, nlevel = 4, horizontal = FALSE, legend.shrink = 0.7)
My problem is like the figure below, the colorbar interacts with the plot.
Then I found some solution to move the colorbar by
image.plot(legend.only = TRUE, zlim = c(0, 1000),
col = colorbar, nlevel = 4, horizontal = FALSE, legend.shrink = 0.7, smallplot = c(0.95, 0.99, 0.3, 0.7))
But now it becomes this, right now I cannot even see the numbers.
Is there any way to move the plot a little left, so that there is some place for the colorbar? Or is there any way to expand the drawing panel to place the colorbar? Thank you.
Adjust the oma for the image and the legend. Here's an example:
data:
structure(c(1.75267599319517, 1.94305075507445, -0.156487381738027,
0.445640426661944, 1.47524414033855, 1.02750392394893, -1.22677726427257,
0.328939164881444, 1.8373305915836, -2.60705394935169, -0.176565075649185,
0.686525129060304, -0.270880825694669, 0.228427158100646, 0.296087431746941,
0.159733127698645, -0.335874862599458, -1.48189689355783, 2.28164829999553,
-0.221602702990901, -1.19118638639445, -0.669176494493408, 0.146646167689706,
-0.356170596317782, -1.25338279788711, 1.590956130167, -0.0859783795499388,
1.28699804800428, 0.673030469365775, 1.50431821599456, -0.0415374929905405,
-1.66641620237076, -0.466513627830826, 0.224700229437091, -0.0935518596695337,
1.77372649149139, -0.324986645432598, -0.717452050358237, -0.652792543676794,
-0.63363234302696, -1.33373586598015, 0.287736774848359, -2.0047033043693,
-0.169110483354588, -1.00746374158438, 1.09791123137282, 0.835200025940813,
1.49766948516664, -0.372579472535408, 0.0928212636896341, -0.596541205386888,
-2.08914364716957, -0.0329161555494402, 0.0469126764319172, 0.901591475473942,
-1.81735802943062, 0.0116636902497983, -0.857668758902046, -0.0123288459943967,
-2.06908827360805, -2.04305175171055, 0.230800348099395, -0.548843960606738,
0.42156183975044, 0.0374174028221, -0.564919467814281, -1.48228025377204,
-0.687727667103207, 1.28230405559294, 0.680295500103408, -2.14368865783057,
0.259858393651711, -1.01732304373518, -0.0188730011118445, -0.386878321355593,
-0.810280425180489, 0.706333208066621, 0.783769913289453, -0.245972056614985,
0.216018643466252, -0.721116233056827, 1.4782390752923, -2.08847116325789,
0.924181052813952, 0.740754382565328, 0.537848528023596, -1.40878671892895,
-0.0870042863455953, 0.797988334415009, -0.601329461472177, -1.15426452376043,
1.28814038900589, -0.997765393519507, 1.09387671511455, 0.599442343976457,
0.323252016862286, -0.338054796460218, 1.60358639606103, -0.836761687404309,
0.224602950250348, 0.457867653133552, 0.111812983058114, -1.94611223307409,
0.816617488615419, 0.803506493408523, -3.27213559068651, 0.914990262538904,
1.04878880717756, 1.02930117080295, -0.780637711297084, -1.63107614827176,
-0.780474130437691, 1.75029718369322, 0.896818981075001, -0.388043322063959,
-0.472369396803673, 0.949964242662063, -0.665981283816353, 1.14546357500757,
-0.149619678444026, 1.31405614654295, -0.916785592776755, 0.595768148350207,
0.554906368528589, -0.64216913243052, -0.141294344542147, -1.10328974307499,
-0.0523192529246229, -0.16760238083543, -2.21744159323307, 0.293362942177156,
-0.198123321190054, -1.27367916442953, 1.3475805394527, 0.379260832421235,
-1.21921905754775, -0.715980480102964, -0.719626343992431, 1.1512160588389,
-0.84503724263784, -1.79029200798331, 1.00839282965634, 1.8043734147227,
-1.0819177220181, -0.200947360793273, -0.30353527334045, 0.444037029616591,
-0.478592762144137, 0.834712578703853, -0.119438685285821), .Dim = c(10L, 15L))
Set 4 lines for right side margin:
par(oma=c(0,0,0,4))
image(data)
Reset outside margin for right side before plotting legend:
par(oma=c( 0,0,0,1))
image.plot( legend.only=TRUE, zlim=c(-4,4))

I have almost succeeded in putting latex fonts into R graphs, but it's slightly off

I am learning latex graphics. I generated latex graphs with standalone, but I am trying to generate R plots with latex fonts. Through online tutorials, here is my code with the Iris dataset in RStudio (I modified example code to get it working. Once I know how to fix the frame, I can study the code in more details):
library(tikzDevice)
options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}",
"\\usepackage{amssymb}"))
## I need the amssymb package because I use \mathcal and \mathbb
tikz("formula.tex", width = 4, height = 4, standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}"))
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
library(tikzDevice)
options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}",
"\\usepackage{amssymb}"))
## I need the amssymb package because I use \mathcal and \mathbb
tikz("formula.tex", width = 4, height = 4, standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}"))
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
plot(iris$Sepal.Length, iris$Sepal.Width, main="Iris sepal length vs width measurements", xlab="Length", ylab="Width")
dev.off()
tools::texi2pdf("formula.tex")
system(paste(getOption("pdfviewer"), "formula.pdf"))
Which gives me:
I would like to have a bit more white space on the right, so the 8 comes fully, and fix the title. The inner picture should be smaller and the title lower as well, if possible.
Sorry for this. I looked at the code and figured it out! I needed to remove
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
which gives me:
So it works!!! :D

R - add title to images with rasterImage

I have 12 twelve PNG files which I want to combine in a single plot with 4x3 grid in R.
So far I can create the grid with,
plot(c(0,4), c(0,3), type = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
and I can add images to it with,
rasterImage(readPNG("image1.png"), 0, 3, 1, 2)
rasterImage(readPNG("image2.png"), 1, 3, 2, 2)
etc.
I get what I want, but I also want to add a title to each image in the plot. Like image1 should have a. Image1 and image2 should have b. Image2 on top of the images. Is there a way to do in R?
Thanks in advance.
#BondedDust's suggestion to use text is perfect, but using the mfrow (or mfcol) graphic parameter in par to layout the grid of plots might be sensible. You can then use plot(..., main='foo') or title(main='foo') to add the titles. For example:
Download some example png graphics, and read them into a list:
library(png)
pngs <- lapply(LETTERS[1:12], function(x) {
u <- 'http://icons.iconarchive.com/icons/mattahan/umicons/64'
download.file(mode='wb', sprintf('%s/Letter-%s-icon.png', u, x),
f <- tempfile(fileext='.png'))
readPNG(f)
})
Use mfrow to set the plot to have 4 rows and 3 columns, and add an upper margin for titles with mar. Then use sapply (for example) to iterate over elements of pngs (well, actually the indexes, 1 through 12, of the elements), plotting each in turn:
par(mfrow=c(4, 3), mar=c(0, 0, 3, 0))
sapply(seq_along(pngs), function(i) {
plot.new()
plot.window(xlim=c(0, 1), ylim=c(0, 1), asp=1)
rasterImage(pngs[[i]], 0, 0, 1, 1)
title(paste0(letters[i], '. Image ', i), font.main=2)
})
Try this:
text(x=0.5,y=2.95, labels="a. Image1")
text(x=1.5,y=2.95, labels="b. Image1")
If it needed to be bold, then plotmath expressions are needed:
text(x=1.5,y=2.95, labels=expression( bold(b.~Image1) ) )

Resources