I have created 36 heatmaps with the function pheatmap, and I want to display them in just one figure. I have tried to using the function par(), but it did not work, I do not know why. Could someone tell me what should I do? Thank you very much. This is my code:
require(graphics);require(grDevices);library("pheatmap", lib.loc="D:/Program Files/R/R-3.1.1/library");library(gplots)
filenames<-list.files("D:/Project/bladder cancer/heatmap0829/heatmap/"); # detect all of the files in the fold
filename2<-strtrim(filenames,nchar(filenames)-4); # all of the filenames without extension names
par(mfrow=c(18,2)) #divide the graphics windows into a 18x2 matrix
for(i in 1:length(filename2)){
rt<-read.table(paste("D:/Project/bladder cancer/heatmap0829/heatmap/",filenames[i],sep = ""), header = T, sep = '\t') # Import the data with the ith file name
size=dim(rt) # the dimensional of the datafram
cw=400/size[1] #the width of the cell in the heatmap
rt<-log10(rt)
x <- t(data.matrix(rt))
pheatmap(x,color=greenred(256),main=filename2[i],cluster_rows = F, cluster_cols = T,cellwidth = cw, cellheight = 60,border_color =F,fontsize = 8,fontsize_col = 15)}
This is one dataset
ScaBER 5637
1 1.010001e+02
1.341186e+00 2.505067e+01
1.669456e+01 8.834190e+01
7.141351e+00 3.897474e+01
1.585592e+04 5.858210e+04
1 3.137979e+01
1.498863e+01 7.694948e+01
1.115443e+02 3.642917e+02
1.157677e+01 5.036716e+01
4.926492e+02 8.642784e+03
3.047117e+00 1.872154e+01
I have 36 txt files like this, but I can not put all of them here
"ScaBER 5637" is the column name of this dataset
See this previous answer: Histogram, error: Error in plot.new() : figure margins too large
par(mfcol=c(3,12), oma=c(1,1,0,0), mar=c(1,1,1,0), tcl=-0.1, mgp=c(0,0,0))
for(i in 1:36){
plot(runif(2), runif(2), type="l")
}
dev.off()
Related
This is my very first post and I'm also really new to R (and programming in general).
I've been trying all I've been finding, with posts like this one:
heatmap in R how to resize columns labels?
But nothing seems to work to me.
The problem here is that the table data that I have to use is kind of big (17 columns and 107 rows) and the labels for the rows are huge..
I need all the columns and rows labels to be clearly seen and distinguished from each other, as well as the rows. Every cell of the heatmap counts.
After running the code that was answered in the previous post for a pheatmap, I get this error (I'm gonna paste all the console says):
> library(pheatmap)
> library(gplots)
> x <- read.table(file='data1.tsv', sep = '\t', header = TRUE, fill = TRUE, row.names = 1, check.names = FALSE)
>
> if (nrow(x) > 100) stop("")
Error:
> fontsize_row = 10 - nrow(x) / 15
> pheatmap(x, col=greenred(256), main='Experiment', cluster_cols=F,
+ fontsize_row=fontsize_row, wordLengths=c(0,Inf), border_color=NA)
Error in strwidth(t, units = "in", cex = fontsize_row/fontsize) :
valor de 'cex' no válido
And this is the code I've been using
library(pheatmap)
library(gplots)
x <- read.table(file='data1.tsv', sep = '\t', header = TRUE, fill = TRUE, row.names = 1, check.names = FALSE)
if (nrow(x) > 100) stop("")
fontsize_row = 10 - nrow(x) / 15
pheatmap(x, col=greenred(256), main='Experiment', cluster_cols=F,
fontsize_row=fontsize_row, wordLengths=c(0,Inf), border_color=NA)
Obviously I downloaded the packages. I also have normal heatmap and heatmap.3 if that would give a better result...
Everything leading to a correct solution will help me a lot !
I attach a picture of the result and the core problem:
· Not all row labels are shown (maybe they should not appear since they
are a lot but...). And, its name gets cut
· Column labels are only 17. They need to be all seen and only few are
shown
IMAGE.PDF WITH THE BAD RESULT I'M GETTING
This is the data1.tsv file that I have to use in case someone wants to try:
https://gofile.io/d/Zqc3ai
I guess the answer is easier than what I imagine but I just don't find it...
Thank you very much !
I have 24 variables called empl_1 -empl_24 (e.g. empl_2; empl_3..)
I would like to write a loop in R that takes this values 1-24 and puts them in the respective places so the corresponding variables are either called or created with i = 1-24. The sample below shows what I would like to have within the loop (e.g. ye1- ye24; ipw_atet_1 - ipw_atet_14 and so on.
ye1_ipw <- empl$empl_1[insample==1]
ipw_atet_1 <- treatweight(y=ye1_ipw, d=treat_ipw, x=x1_ipw, ATET =TRUE, trim=0.05, boot = 2)
ipw_atet_1
ipw_atet_1$se
ye2_ipw <- empl$empl_2[insample==1]
ipw_atet_2 <- treatweight(y=ye2_ipw, d=treat_ipw, x=x1_ipw, ATET =TRUE, trim=0.05, boot = 2)
ipw_atet_2
ipw_atet_2$se
ye3_ipw <- empl$empl_3[insample==1]
ipw_atet_3 <- treatweight(y=ye3_ipw, d=treat_ipw, x=x1_ipw, ATET =TRUE, trim=0.05, boot = 2)
ipw_atet_3
ipw_atet_3$se
coming from a Stata environment I tried
for (i in seq_anlong(empl_list)){
ye[i]_ipw <- empl$empl_[i][insample==1]
ipw_atet_[i]<-treatweight(y=ye[i]_ipw, d=treat_ipw, x=x1_ipw, ATET=TRUE, trim=0.05, boot =2
}
However this does not work at all. Do you have any idea how to approach this problem by writing a nice loop? Thank you so much for your help =)
You can try with lapply :
result <- lapply(empl[paste0('empl_', 1:24)], function(x)
treatweight(y = x[insample==1], d = treat_ipw,
x = x1_ipw, ATET = TRUE, trim = 0.05, boot = 2))
result would be a list output storing the data of all the 24 variables in same object which is easier to manage and process instead of having different vectors.
I am relatively new to R, and it seems that, despite my loops working properly otherwise, I am unable to iterate trough a list to create pdf:
For instance this code
(Variables & libraries:)
Libraries
library(Seurat)
The different markers are lists of chain of characters like DenditicCells:
DendriticCells <- c("Kmo", "Flt3", "Ccr7", "Ccl17", "Irf8","Xcr1","Cd209")
Markers <- list(Neurons, Oligo, OPC, AstroPro, Astro, OligoPro, Pericytes, ImmuneCells, GeneOfInterest, Lymphatics, Vein, Arteries, cappilaries, Microglial, Macrophages, ThCells, Tcells, Bcells, Granulocytes, DendriticCells, CPMicrogenes, TNK, migDCs )
Markers <- setNames(Markers, c("Neurons", "Oligo", "OPC", "AstroPro", "Astro", "OligoPro", "Pericytes", "ImmuneCells", "GeneOfInterest", "Lymphatics", "Vein", "Arteries", "cappilaries", "Microglial", "Macrophages", "ThCells", "Tcells", "Bcells", "Granulocytes", "DendriticCells", "CPMicrogenes", "TNK", "migDCs" ))
Code
pdf(paste0("Run5/DotPlot6", names(Markers[x]),"Subset4.jpeg"))
DotPlot(Subset4, assay = "SCT" ,features =Markers[[x]], dot.scale = 8)
dev.off()
Works and creates a pdf, but this code:
Ret4 <- function(x){
pdf(paste0("Run5/DotPlot6", names(Markers[x]),"Subset4.jpeg"))
try(DotPlot(Subset4, assay = "SCT" ,features =Markers[[x]], dot.scale = 8))
dev.off()
}
for(i in 1:length(Markers))Ret4(i)
fails after a perfectly normal execution. I have tried variation using different format, lapply, map, and it does not work. I do not understand why this execution fails...
How can i iterate through this? In this case, Markers is a list of list of 24 elements.
Thanks a lot
Jean
solution:
Ret5 <- function(x, Markers, Subset, nameSubset){ p <- DotPlot(Subset, assay = "SCT" ,features =Markers[[x]], dot.scale = 8)
png(paste("Run5/Subset/", as.character(x),names(Markers[[x]]),".jpeg", sep = ""))
print(p)
dev.off() }
for(x in c(1:length(Markers))){ Ret5(x, Markers, Subset1, "Subset1")}
I'm trying to optimize the parameters for baseline in the R baseline package by changing each parameters in a loop and comparing plots to determine which parameters give me the best baseline.
I currently have the code written so that the loop produces each plot, but I'm having trouble with getting the plot saved as the class of each object I'm creating is a baseline package-specific (which I'm suspecting is the problem here).
foo <- data.frame(Date=seq.Date(as.Date("1957-01-01"), by = "day",
length.out = ncol(milk$spectra)),
Visits=milk$spectra[1,],
Old_baseline_visits=milk$spectra[1,], row.names = NULL)
foo.t <- t(foo$Visits)
#the lines above were copied from https://stackoverflow.com/questions/37346967/r-packagebaseline-application-to-sample-dataset to make a reproducible dataset
df <- expand.grid(lambda=seq(1,10,1), p=seq(0.01,0.1,0.01))
baselinediff <- list()
for(i in 1:nrow(df)){
thislambda <- df[i,]$lambda
thisp <- df[i,]$p
thisplot <- baseline(foo.t, lambda=thislambda, p=thisp, maxit=20, method='als')
print(paste0("lambda = ", thislambda))
print(paste0("p = ", thisp))
print(paste0("index = ", i))
baselinediff[[i]] <- plot(thisplot)
jpeg(file = paste(baselinediff[[i]], '.jpeg', sep = ''))
dev.off()
}
I know that I would be able to extract corrected spectra using baseline.als but I just want to save the plot images with the red baseline so that I can see how well the baselines are getting drawn. Any baseline users out there that can help?
I suggest you change your loop in the following way:
for(i in 1:nrow(df)){
thislambda <- df[i,]$lambda
thisp <- df[i,]$p
thisplot <- baseline(foo.t, lambda=thislambda, p=thisp, maxit=20, method='als')
print(paste0("lambda = ", thislambda))
print(paste0("p = ", thisp))
print(paste0("index = ", i))
baselinediff[[i]] <- thisplot
jpeg(file = paste('baseline', i, '.jpeg', sep = ''))
plot(baselinediff[[i]])
dev.off()
}
Note that this does not try to capture the already plotted element (thisplot) inside of the list. Instead, the plotting is done after you call the jpeg command. This solves your export issue. Another problem was the naming of the file. If you call baselinediff[[i]] inside of paste, you apparently end up with an error. So I switched it to a simpler name. To plot your resulting list, call:
lapply(baselinediff, plot)
If you are determined on storing the already plotted element, the capture.plotfunction from the imager package might be a good start.
I think I have read every page on the internet that mentions coldiss and I am still having trouble getting the labels to look correctly. In the image I inserted, the matrices look good but the labels are default numbers (so aren't that useful for a stand alone image) and in the ordered matrix the matrix gets ordered correctly, but the labels didn't re-order, which doesn't make sense.
[Matrix output images][1]
My questions are:
1) How do I get the labels to order properly for the ordered matrix? If the cells in the heat map are changing colors after being ordered, the respective labels should be different too.
2) Is it possible to edit the coldiss function to use my isolate labels that can be found in the top row or first column to label the heat map rather than the default numbers?
Here is the code I'm running.
library(gclus)
library(ape)
source("coldiss.txt")
tree<-read.tree("BP_SNPS_only-BioNJ_tree_100BS")
PatristicDistMatrix100BS<-cophenetic.phylo(tree)
coldiss(D = PatristicDistMatrix100BS, nc = 4, byrank = TRUE, diag = TRUE)
Here is the coldiss.txt file:
# coldiss()
# Color plots of a dissimilarity matrix, without and with ordering
#
# License: GPL-2
# Author: Francois Gillet, 23 August 2012
#
"coldiss" <- function(D, nc = 4, byrank = TRUE, diag = FALSE)
{
require(gclus)
if (max(D)>1) D <- D/max(D)
if (byrank) {
spe.color <- dmat.color(1-D, cm.colors(nc))
}
else {
spe.color <- dmat.color(1-D, byrank=FALSE, cm.colors(nc))
}
spe.o <- order.single(1-D)
speo.color <- spe.color[spe.o, spe.o]
op <- par(mfrow=c(1,2), pty="s")
if (diag) {
plotcolors(spe.color, rlabels=attributes(D)$Labels,
main="Dissimilarity Matrix",
dlabels=attributes(D)$Labels)
plotcolors(speo.color, rlabels=attributes(D)$Labels[spe.o],
main="Ordered Dissimilarity Matrix",
dlabels=attributes(D)$Labels[spe.o])
}
else {
plotcolors(spe.color, rlabels=attributes(D)$Labels,
main="Dissimilarity Matrix")
plotcolors(speo.color, rlabels=attributes(D)$Labels[spe.o],
main="Ordered Dissimilarity Matrix")
}
par(op)
}
# Usage:
# coldiss(D = dissimilarity.matrix, nc = 4, byrank = TRUE, diag = FALSE)
# If D is not a dissimilarity matrix (max(D) > 1), then D is divided by max(D)
# nc number of colours (classes)
# byrank= TRUE equal-sized classes
# byrank= FALSE equal-length intervals
# diag = TRUE print object labels also on the diagonal
# Example:
# coldiss(spe.dj, nc=9, byrank=F, diag=T)
Here is an abbreviated version of PatristicDistMatrix100BS:
CDC-B043_1995 CDC-A267_1994 CDC-A161_1992 CDC-C931_1998
CDC-B043_1995 0 0.00099 0.00099 0.00166
CDC-A267_1994 0.00099 0 0.00066 0.00133
CDC-A161_1992 0.00099 0.00066 0 0.00133
CDC-C931_1998 0.00166 0.00133 0.00133 0
I hope this provides all the relevant information and thank you for any help you can provide even if it's a completely different function.
There is nothing wrong in the code. The main problem I think is some other packages you have loaded. I also had same problem but when I tried separately it worked well and as you require. Just remove other packages or calculate separately. For more details have a look on the code of chapter three of this document (http://adn.biol.umontreal.ca/~numericalecology/numecolR/). Here is the code I work with.
(vegan must be loaded after ade4 to avoid some conflicts)
library(ade4)
library(vegan)
library(gclus)
library(cluster)
library(FD)
files must be in the working directory. You can search this file from internet from this link (https://github.com/JoeyBernhardt/NumericalEcology)
source("coldiss.R")
source("panelutils.R")
Then calculate your dissimilarity matrix and plot using the code
BCD <- vegdist(df[-1])
coldiss(BCD, byrank = FALSE, diag = TRUE)
Hopefully it will work.