R pheatmap scale different than scale before pheatmap - r

The heatmap when scaling before plotting:
mat_scaled <- scale(t(mat))
pheatmap(t(mat_scaled), show_rownames=F, show_colnames=F,
border_color=F, color=colorRampPalette(brewer.pal(6,name="PuOr"))(12))
with the scale going from [-2, 6] is completely different than when using the scaling within the pheatmap function
pheatmap(t(mat_scaled), scale="row", show_rownames=F,
show_colnames=F, border_color=F, color=colorRampPalette(brewer.pal(6,name="PuOr"))(12))
where the scale is set from [-6,6].
Why is this difference and how could I obtain the matrix represented in the second figure?

In the second figure you plot the heatmap of the scaled matrix mat_scaled scaled a second time using the option scale="row" of pheatmap.
This is not the right way to compare external and internal scaling.
Here is the solution:
library(gridExtra)
library(pheatmap)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(6,name="PuOr"))(12)
brks <- seq(-3,3,length.out=12)
data(attitude)
mat <- as.matrix(attitude)
# Scale by row
mat_scaled <- t(scale(t(mat)))
p1 <- pheatmap(mat_scaled, show_rownames=F, show_colnames=F,
breaks=brks, border_color=F, color=cols)
p2 <- pheatmap(mat, scale="row", show_rownames=F, show_colnames=F,
breaks=brks, border_color=F, color=cols)
grid.arrange(grobs=list(p1$gtable, p2$gtable))

Related

Scaled plotting of multiple pairwise Venn diagrams in R

I want to plot >50 Venn/Euler diagrams of two sets each to scale.
It is necessary that not only the overlap of the two sets and the set size themselves should scale but also the size of the individual diagrams compared to each other.
Since I know of no R package that allows the plotting of >50 pairwise Venn diagrams at the same time, I was planning to plot them first individually (e.g., using eulerr) and then put all of them together using the gridExtra package or something similar.
However, in this way, the size of the individual pairwise diagrams is not comparable:
require(gridExtra)
require(eulerr)
fit1 <- euler(c(A=300, B=500, "A&B"=100))
fit2 <- euler(c(A=40, B=70, "A&B"=30))
grid.arrange(plot(fit1), plot(fit2), nrow=1)
Does anyone know of an R package or a combination of packages that would allow size-appropriate plotting of several pairwise Venn diagrams?
You could try using the widths argument of grid.arrange. You would have to determine the ratio of each of the venn diagrams' totals. In your example, the total size ratio is 800:110, which is 7.27, so if you do grid.arrange(plot(fit1), plot(fit2), ncol = 2, widths = c(7.27, 1)) then fit2 will be much smaller than fit1. The ggarrange() function from ggpubr should work also.
fit1 <- euler(c(A=300, B=500, "A&B"=100))
fit2 <- euler(c(A=40, B=70, "A&B"=30))
tot1 <- 800
tot2 <- 110
ratio_v <- tot1/tot2
grid.arrange(plot(fit1), plot(fit2), ncol = 2, widths = c(ratio_v, 1))
ggpubr:ggarrange(plotlist = list(plot(fit1), plot(fit2)), ncol = 2, widths = c(ratio_v, 1))
Edit: Want the individual pairwise sets to have their own size ratios, instead of everything relative to a global maximum. This is a simple example, but you can write a function to do this automatically for each one. Basically set the maximum number of columns (I just chose 100), and then convert each of your ratios to be out of 100. Make a row for each venn diagram set, then rbind them all into a matrix and use the layout_matrix argument.
### Make fits
fit1 <- euler(c(A=300, B=500, "A&B"=100))
fit2 <- euler(c(A=40, B=70, "A&B"=30))
fit3 <- euler(c(C=100, D=300, "C&D"=50))
fit4 <- euler(c(C=50, D=80, "C&D"=30))
### Assign totals
tot1 <- 800
tot2 <- 110
tot3 <- 400
tot4 <- 130
### Find ratios
ratioAB_v <- round(tot1/tot2)
ratioCD_v <- round(tot3/tot4)
### Convert ratios
smallAB_v <- round(1/ratioAB_v*100)
smallCD_v <- round(1/ratioCD_v*100)
### Make rows
row1_v <- c(rep(1, (100-smallAB_v)), rep(2, smallAB_v))
row2_v <- c(rep(3, (100-smallCD_v)), rep(4, smallCD_v))
### Make matrix
mat <- rbind(row1_v, row2_v)
### Plot
grid.arrange(plot(fit1), plot(fit2), plot(fit3), plot(fit4), layout_matrix = mat)

Dotchart with secondary axis

I'm trying to produce a dotchart with a secondary axis on top. However once I plot the second dotchart (with a par(new=T)), I can't figure out how not to display the axis ticks over the previous ones in axis side=1. Here's my code with mock data:
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
dotchart(y1_i,labels=x,xlab="N50 length",xlim = c(0,max(y1)))
par(new=T)
dotchart(y2_i,labels=x,xlim = c(0,max(y2)))
axis(side=3)
Also, if possible, I would like to add a second data set which would be slightly pushed vertically above the first dataset (to not overlap it), but still corresponding to the same y-axis categories.
Thank you for any suggestion :)
Found it, by using dotchart2 from the Hmisc package
library(Hmisc)
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
y1_i <- c(2,8,2,14,2)
y2_i <- c(15,17,28,22,30)
y1_f <- c(4,9,11,16,7)
y2_f <- c(13,11,16,11,21)
y=c(y1_i,y2_i,y1_f,y2_f)
x <- c("AAEG","AALO","AGAM","ACHR","AALB")
y1=c(y1_i,y1_f)
y2=c(y2_i,y2_f)
dotchart2(y1_i,labels=x,xlab="N50 length",xlim = c(0,max(y1)))
par(new=T)
dotchart2(y2_i,labels=x,xlim = c(0,max(y2)),xlab="Scaffold number",lines=F,xaxis=F)
axis(side=3,xlab="Scaffold number")

Plotting standard error on radial.plot()

I am using the radial.plot function of the plotrix-package in R. Does anyone know of a straightforward way to implement standard-error bars. The solution would have to work even when there is more than one datapoint per radial position which could lead to partial overlap of the SE-bar (see graph below).
Now the graph looks like this:
Used Code:
library(plotrix)
ppp <- matrix(runif(1:16, 10, 60), nrow=2, ncol=8)
kl <- c(0, pi/4, pi/2, pi*0.75,pi, pi+pi/4,pi+pi/2, pi+pi*0.75)
plot_rt_soa7 <- radial.plot(ppp,rp.type="p",radial.pos=kl,
label.pos=kl,start=pi/2,
labels=1:8,radial.lim=c(-10,65),main="SOA 7")
legend(45,50,c("T-oben", "T-unten"),col=1:2,lty=1)
The errorbars could look e.g. like this:
(from How to plot error bars in polar coordinates in python?)
Any help would be much appreciated
Here is some basic code that will plot error bars for both the 'x' (orthogonal to radius) and 'y' (parallel to radius) dimensions, and a point for the center value. It does not use the plotrix package for the error bar plotting, but instead uses R base graphics. You must provide the errors for the dimensions or comment out the part of the code that plots undesired errors. There are several graphical parameters for line weight, color, point color, and point shape. A sample graph is provided below.
library(plotrix)
set.seed(10) # seed for reproducable graph
ppp <- matrix(runif(1:16, 10, 60), nrow=2, ncol=8)
kl <- c(0, pi/4, pi/2, pi*0.75,pi, pi+pi/4,pi+pi/2, pi+pi*0.75)
start <- pi/2 # know starting value for plotting points angularl
rad_low_lim <- -10 # used when computing values of the error lines and in plot limits
plot_rt_soa7 <- radial.plot(ppp,rp.type="p"
,radial.pos=kl
,label.pos=kl
,start=start
,labels=1:8
,radial.lim=c(rad_low_lim,65)
,main="SOA 7")
legend(40,120,c("T-oben", "T-unten"),col=1:2,lty=1)
# generating random error values for both x and y
error_ppp_y <- matrix(rnorm(16, 15, 5), nrow=2, ncol=8)
error_ppp_x <- matrix(rnorm(16, 10, 3), nrow=2, ncol=8)
bar_cols <- c('blue','green') # colors for bars
lwds <- c(4,2) # line weights for bars
pts_cols <- c('black','red') # colors for points
pts_pch <- c(19,17) # point pch
# loop over the number of rows (T-oben and T-unten)
for(j in 1:2){
# loop over the observations
for(i in 1:ncol(ppp)){
# plotting the errors of the 'y' value
# center value is determined and errors are rotated to make
# parallel to the radius
lines(c(ppp[j,i]+error_ppp_y[j,i]-rad_low_lim,ppp[j,i]-error_ppp_y[j,i]-rad_low_lim)*cos(kl[i]+start)
,c(ppp[j,i]+error_ppp_y[j,i]-rad_low_lim,ppp[j,i]-error_ppp_y[j,i]-rad_low_lim)*sin(kl[i]+start)
,lwd=lwds[j]
,col=bar_cols[j]
)
# plotting the 'x' errors that are orthognal to the radius
# points are the "center" with the error values rotated to make them orthognal to the radius
# comment out if not desired
lines((ppp[j,i]-rad_low_lim)*cos(kl[i]+start)+c(error_ppp_x[j,i],-error_ppp_x[j,i])*cos(kl[i])
,(ppp[j,i]-rad_low_lim)*sin(kl[i]+start)+c(error_ppp_x[j,i],-error_ppp_x[j,i])*sin(kl[i])
,lwd=lwds[j]
,col=bar_cols[j]
)
# plotting points for the center
# comment out if not desired
points((ppp[j,i]-rad_low_lim)*cos(kl[i]+start)
,(ppp[j,i]-rad_low_lim)*sin(kl[i]+start)
,col=pts_cols[j]
,pch=pts_pch[j]
)
}
}

Stacking of several Surface plots in 3D-View

Lets consider that I have five 2D-Matrices which describe the magnetic field at different z-Layers. A nice, smoothed version of a 2D-Surface plot can be obtained as follows:
data2_I<-matrix(c(1.0,1.0,0.6,0.6,0.7,0.9,0.9,0.5,0.5,0.5,0.7,0.9,0.9,0.6,0.3,0.4,0.7,0.9,0.9,0.7,0.5,0.5,0.6,0.9,0.9,0.7,0.6,0.6,1.0,1.0), nrow=5)
Z = as.vector(data2_I)
length(Z)
XY=data.frame(x=as.numeric(gl(5,1,30)),y=as.numeric(gl(5,6,30)))
t=Tps(XY,Z)
surface(t)
Now it would be great if I could get a 3D-plot where at different z-Positions these surfaces are plotted. Is there a possibility to do that?
I found an alternative approach: With the package rgl I and the function surface 3D I can stack several 3D-Surface plots within one open3d-window. Lets look at a small example:
library("rgl")
data2_I<-matrix(c(1.0,1.0,0.6,0.6,0.7,0.9,0.9,0.5,0.5,0.5,0.7,0.9,0.9,0.6,0.3,0.4,0.7,0.9,0.9,0.7,0.5,0.5,0.6,0.9,0.9,0.7,0.6,0.6,1.0,1.0), nrow=5)
data0_I<-matrix(c(1.0,1.0,0.6,0.6,0.7,0.9,0.9,0.5,0.5,0.5,0.7,0.9,0.9,0.6,0.3,0.4,0.7,0.9,0.9,0.7,0.5,0.5,0.6,0.9,0.9,0.7,0.6,0.6,1.0,1.0), nrow=5)
data1_I<-2*data0_I
data2_I<-1/data1_I
elv=0
offs=5*elv+1
z0 <- scale*data0_I
z1 <- scale*data1_I
z2 <- scale*data2_I
x <- 1:nrow(z0)
y <- 1:ncol(z0)
palette <- colorRampPalette(c("blue","green","yellow", "red"))
col.table <- palette(256)
open3d(windowRect=c(50,50,800,800))
surface3d(x, y, elv*z0, color = col.table[cut(z0, 256)], back = "lines")
surface3d(x, y, elv*z1+1*offs, color = col.table[cut(z1, 256)], back = "lines")
surface3d(x, y, elv*z2+2*offs, color = col.table[cut(z2, 256)], back = "lines")
axes3d()
aspect3d(1,1,2)
The variables offsand elv are included for cosmetic purposes: offs controls the space between two surface plots and elevation how the z-axes of the surface3d-plots should scale. As I wanted to have a 2D surface plot without any elevation I set it to zero.

R: Centering heatmap.2 key (gplots package)

I would like to create a heatmap via the heatmap.2() command with a color key that is centered on 0 (i.e. white color -> 0, red -> greater than 0, blue -> less than 0) while keeping scale="none" as I am interested in plotting a heatmap of the actual values. However, all of my heatmaps are not centered on zero upon using the following line:
library(gplots)
outputHeatmap <- heatmap.2(heatmapInputActual, dendrogram="none", Rowv=FALSE,
Colv=FALSE, col= bluered(256), scale="none", key=TRUE, density.info="none",
trace="none", cexRow=0.125, cexCol=0.125, symm=FALSE, symkey=TRUE)
I thought that using the command symkey=TRUE would work, but it does not. The variable I am trying make a heatmap of is an (n x 3) matrix of numerical values. A problematic input to the heatmap.2() command described above follows:
8.408458 5.661144 0.00000000
4.620846 4.932283 -0.46570468
-4.638912 -3.471838 -0.12146109
-4.822829 -3.946024 0.06403327
3.948832 4.520447 -0.31945941
Thank you for your time. I look forward to your replies.
The solution seem to be just adding symbreaks to your heatmap.2. Here is a fully reproducible example with your data:
library(gplots)
#read your example data
heatmapInputActual <- read.table(textConnection(
"8.408458 5.661144 0.00000000
4.620846 4.932283 -0.46570468
-4.638912 -3.471838 -0.12146109
-4.822829 -3.946024 0.06403327
3.948832 4.520447 -0.31945941
"),as.is=TRUE)
#convert sample data to matrix
heatmapInputActual <- as.matrix(heatmapInputActual)
#just add symbreaks to the end of your code
heatmap.2(heatmapInputActual, dendrogram="none", Rowv=FALSE, Colv=FALSE,
col = bluered(256), scale="none", key=TRUE, density.info="none",
trace="none", cexRow=0.125, cexCol=0.125, symm=F,symkey=T,symbreaks=T)

Resources