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))
Related
To expand upon visualize a list of colors/palette in R I am trying to display a series of custom colour palettes in R in a single figure. Is there a way that I can expand on one of the methods listed in the link to display the list of palettes below:
convert_coolers <- function(coolers_string){
strsplit(coolers_string, split = ", ")[[1]]
}
# diverging
storm_panels <- convert_coolers("#001219, #005f73, #0a9396, #94d2bd, #e9d8a6, #ee9b00, #ca6702, #bb3e03, #ae2012, #9b2226")
harry_tipper <- convert_coolers("#f72585, #b5179e, #7209b7, #560bad, #480ca8, #3a0ca3, #3f37c9, #4361ee, #4895ef, #4cc9f0")
firepit <- convert_coolers("#03071e, #370617, #6a040f, #9d0208, #d00000, #dc2f02, #e85d04, #f48c06, #faa307, #ffba08")
# sequences
the_deep <- convert_coolers("#03045e, #023e8a, #0077b6, #0096c7, #00b4d8, #48cae4, #90e0ef, #ade8f4, #caf0f8")
earth <- convert_coolers("#ede0d4, #e6ccb2, #ddb892, #b08968, #7f5539, #9c6644")
# categorical
pastal_rainbow <- convert_coolers("#ff595e, #ffca3a, #8ac926, #1982c4, #6a4c93")
fisherman <- convert_coolers("#353535, #3c6e71, #ffffff, #d9d9d9, #284b63")
in a figure resembling that displayed by RColorBrewer::display.brewer.all()? i.e. with palettes stacked as horizontal bars labelled to the left with the palette title.
I have been trying to dissect the method out from the RColorBrewer function but am finding that it depends too much on internal variables for me to understand what is going on.
I achieved what I set out to do by modifying RColorBrewer::display.brewer.all
Following directly on from the code in the question:
display_custom_palettes <- function(palette_list, palette_names){
nr <- length(palette_list)
nc <- max(lengths(palette_list))
ylim <- c(0, nr)
oldpar <- par(mgp = c(2, 0.25, 0))
on.exit(par(oldpar))
plot(1, 1, xlim = c(0, nc), ylim = ylim, type = "n", axes = FALSE,
bty = "n", xlab = "", ylab = "")
for (i in 1:nr) {
nj <- length(palette_list[[i]])
shadi <- palette_list[[i]]
rect(xleft = 0:(nj - 1), ybottom = i - 1, xright = 1:nj,
ytop = i - 0.2, col = shadi, border = "light grey")
}
text(rep(-0.1, nr), (1:nr) - 0.6, labels = palette_names, xpd = TRUE,
adj = 1)
}
plot.new()
palette_list <- list(storm_panels, harry_tipper, firepit, the_deep, earth, pastal_rainbow, fisherman)
palette_names <- c("storm panels", "harry tipper", "firepit", "the deep", "earth", "rainbow", "fisherman")
display_custom_palettes(palette_list, palette_names)
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.
I'm learning how to use R and for an exercise I'm using an experimental ExpresionSet from Bioconductor: http://bioconductor.org/packages/release/data/experiment/html/leukemiasEset.html
As simple as that: My heat map doesn't display the color key and the main title appears chopped off on the left site. I had given up with the no color key problem after searching on the web how to solve it and not really finding a solution. But then I added the title and since it doesn't appear properly either, I thought that maybe both things are due to the same (unknown for me) reason...
Here is my code:
difexp <- exprs(leukemiasEset)[c(which(adjPval < 0.01)),c(1:12, 25:36)]
heatmap.2(difexp,
trace = "none",
cexCol = 0.6,
ColSideColors = as.character(as.numeric(factor(leukemiasEset$LeukemiaType[c(1:12, 25:36)]))),
main = "Differentially expressed genes in ALL and CLL samples",
cex.main = 1.5,
key = TRUE,
keysize = 1.5,
density.info = "histogram")
Where:
pvalues <- c()
for(i in 1:nrow(exprs(leukemiasEset))) {
R <- t.test(exprs(leukemiasEset)[i, leukemiasEset$LeukemiaType == "ALL"],
exprs(leukemiasEset)[i, leukemiasEset$LeukemiaType == "CLL"],
var.equal = TRUE)
pvalues <- c(pvalues, R$p.value)
}
adjPval <- p.adjust(pvalues, method = "fdr")
And this is how it looks like:
Since I am a beginner, I think this is probably an "easy peasy" for the experts... thank you very much in advance!
Use "\n" to insert a newline in your title and put half of it on a second line.
heatmap.2(difexp,
trace = "none",
cexCol = 0.6,
ColSideColors = as.character(as.numeric(factor(leukemiasEset$LeukemiaType[c(1:12, 25:36)]))),
main = "Differentially expressed genes\nin ALL and CLL samples",
cex.main = 1.5,
key = TRUE,
keysize = 1.5,
density.info = "histogram")
I want each rectangle to contain a number, so that the first plotted rectangle would contain : rect 1 the second rect 2 and so on, but i don't know how to insert text inside rectangles.
require(grDevices)
## set up the plot region:
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rect(0.0,0.0,40.0,35.0, , text= "transparent")
rect(10.0,210.0,45.0,230.0)
rect(170.0,50.0,240.0,150.0)
rect(75.0,110.0,125.0,125.0)
rect(50.0,130.0,65.0,160.0)
rect(15.0,140.0,30.0,150.0)
rect(100.0,50.0,130.0,90.0)
rect(150.0,40.0,155.0,60.0)
rect(52.0,80.0,75.0,90.0)
rect(62.0,65.0,85.0,75.0)
rect(20.0,75.0,25.0,80.0)
rect(30.0,40.0,50.0,80.0)
rect(102.0,155.0,113.0,217.0)
par(op)
Like the other answers mention, you can use the coordinates that you give to rect to place the text somewhere relative.
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rect(0.0,0.0,40.0,35.0)
center <- c(mean(c(0, 40)), mean(c(0, 35)))
text(center[1], center[2], labels = 'hi')
You can easily put this into a function to save yourself some typing/errors
recttext <- function(xl, yb, xr, yt, text, rectArgs = NULL, textArgs = NULL) {
center <- c(mean(c(xl, xr)), mean(c(yb, yt)))
do.call('rect', c(list(xleft = xl, ybottom = yb, xright = xr, ytop = yt), rectArgs))
do.call('text', c(list(x = center[1], y = center[2], labels = text), textArgs))
}
Use it like this
recttext(50, 0, 100, 35, 'hello',
rectArgs = list(col = 'red', lty = 'dashed'),
textArgs = list(col = 'blue', cex = 1.5))
You need to use text() as a separate graphics call.
coords <- matrix(
c(0.0,0.0,40.0,35.0,
10.0,210.0,45.0,230.0,
170.0,50.0,240.0,150.0,
75.0,110.0,125.0,125.0,
50.0,130.0,65.0,160.0,
15.0,140.0,30.0,150.0,
100.0,50.0,130.0,90.0,
150.0,40.0,155.0,60.0,
52.0,80.0,75.0,90.0,
62.0,65.0,85.0,75.0,
20.0,75.0,25.0,80.0,
30.0,40.0,50.0,80.0,
102.0,155.0,113.0,217.0),
ncol=4,byrow=TRUE)
plot(c(0, 250), c(0, 250), type = "n",
main = "Exercise 1: R-Tree Index Question C")
rfun <- function(x,i) {
do.call(rect,as.list(x))
}
apply(coords,1,rfun)
text((coords[,1]+coords[,3])/2,
(coords[,2]+coords[,4])/2,
seq(nrow(coords)))
text( (0.0+40.0)/2, (0.0+35.0)/2 , 'transparent')
where we chose x,y to be the centroid of your rectangle. You could define a function to draw the rect then place the text at its centroid.
Note: these coords are large; this will display outside your normal view. So you'll either need to zoom to see it, or scale coords to the range 0.0..1.0
By the way, read 12.2 Low-level plotting commands
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?