plot interaction between two predictors (lme4) with plotLMER.fnc - r

I'm trying to plot the interaction between "condition" and "snarc_num" in the model (built with lme4 package):
quant_num<-lmer(response_time1~condition*snarc_num+(1+condition|subject_nr) + (1|couple), data=quant_slopes)
To plot the interaction I should use the plotLMER.fnc from the languageR package, and I tried:
plotLMER.fnc(quant_num, xlabel = "snarc_num", ylabel ="RTs", ylimit = c(6.5,7.5),
fun = NA, pred = "snarc_num", control =list(quant_slopes$condition, quant_slopes$snarc_num), ranefs = NA, n = 100,
intr =list("condition","snarc_num"), lockYlim = TRUE, addlines = TRUE,
withList = FALSE, cexsize = 0.5, linecolor = 2, addToExistingPlot = FALSE,
verbose = TRUE)
but it doesn't work.
Could anyone help me with this plot?
Thank you!

Related

Heatmap of Gene intensity values in R

I have data that look like this:
Gene
HBEC-KT-01
HBEC-KT-02
HBEC-KT-03
HBEC-KT-04
HBEC-KT-05
Primarycells-02
Primarycells-03
Primarycells-04
Primarycells-05
BPIFB1
15726000000
15294000000
15294000000
14741000000
22427000000
87308000000
2.00E+11
1.04E+11
1.51E+11
LCN2
18040000000
26444000000
28869000000
30337000000
10966000000
62388000000
54007000000
56797000000
38414000000
C3
2.52E+11
2.26E+11
1.80E+11
1.80E+11
1.78E+11
46480000000
1.16E+11
69398000000
78766000000
MUC5AC
15647000
8353200
12617000
12221000
29908000
40893000000
79830000000
28130000000
69147000000
MUC5B
965190000
693910000
779970000
716110000
1479700000
38979000000
90175000000
41764000000
50535000000
ANXA2
14705000000
18721000000
21592000000
18904000000
22657000000
28163000000
24282000000
21708000000
16528000000
I want to make a heatmap like the following using R. I am following a paper and they quoted "Heat maps were generated with the ‘pheatmap’ package76, where correlation clustering distance row was applied". Here is their heatmap.
I want the same like this and I am trying to make one using R by following tutorials but I am new to R language and know nothing about R.
Here is my code.
df <- read.delim("R.txt", header=T, row.names="Gene")
df_matrix <- data.matrix(df)
pheatmap(df_matrix,
main = "Heatmap of Extracellular Genes",
color = colorRampPalette(rev(brewer.pal(n = 10, name = "RdYlBu")))(10),
cluster_cols = FALSE,
show_rownames = F,
fontsize_col = 10,
cellwidth = 40,
)
This is what I get.
When I try using clustering, I got the error.
pheatmap(
mat = df_matrix,
scale = "row",
cluster_column = F,
show_rownames = TRUE,
drop_levels = TRUE,
fontsize = 5,
clustering_method = "complete",
main = "Hierachical Cluster Analysis"
)
Error in hclust(d, method = method) :
NA/NaN/Inf in foreign function call (arg 10)
Can someone help me with the code?
You can normalize the data using scale to archive a more uniform coloring. Here, the mean expression is set to 0 for each sample. Genes lower expressed than average have a negative z score:
library(tidyverse)
library(pheatmap)
data <- tribble(
~Gene, ~`HBEC-KT-01`, ~`HBEC-KT-02`, ~`HBEC-KT-03`, ~`HBEC-KT-04`, ~`HBEC-KT-05`, ~`Primarycells-03`, ~`Primarycells-04`, ~`Primarycells-05`,
"BPIFB1", 1.5726e+10, 1.5294e+10, 1.5294e+10, 1.4741e+10, 2.2427e+10, 2e+11, 1.04e+11, 1.51e+11,
"LCN2", 1.804e+10, 2.6444e+10, 2.8869e+10, 3.0337e+10, 1.0966e+10, 5.4007e+10, 5.6797e+10, 3.8414e+10,
"C3", 2.52e+11, 2.26e+11, 1.8e+11, 1.8e+11, 1.78e+11, 1.16e+11, 6.9398e+10, 7.8766e+10,
"MUC5AC", 15647000, 8353200, 12617000, 12221000, 29908000, 7.983e+10, 2.813e+10, 6.9147e+10,
"MUC5B", 965190000, 693910000, 779970000, 716110000, 1479700000, 9.0175e+10, 4.1764e+10, 5.0535e+10,
"ANXA2", 1.4705e+10, 1.8721e+10, 2.1592e+10, 1.8904e+10, 2.2657e+10, 2.4282e+10, 2.1708e+10, 1.6528e+10
)
data %>%
mutate(across(where(is.numeric), scale)) %>%
column_to_rownames("Gene") %>%
pheatmap(
scale = "row",
cluster_column = F,
show_rownames = FALSE,
show_colnames = TRUE,
treeheight_col = 0,
drop_levels = TRUE,
fontsize = 5,
clustering_method = "complete",
main = "Hierachical Cluster Analysis (z-score)",
)
Created on 2021-09-26 by the reprex package (v2.0.1)

Add Space Between Panels of Openair windRose Plots

I created some windrose plots using the openair package and I'm pretty happy with how they turned out but aesthetically it would be nice to have some space between panels. Here's an example:
# windrose plot----
library(openair)
data("mydata")
windRose(mydata[1:144,], ws="ws", wd="wd",
paddle = F,
type = 'weekday',
key.header = 'Wind Speed (m/s)',
key.footer = "",
annotate = F,
angle = 30, # angle of "spokes"...sort of bins for wind direction
cols = 'jet',
key.position = 'right',
dig.lab = 2,
statistic = 'prop.count', #“prop.count” sizes bins according to the
# proportion of the frequency of measurements
fontsize = 20,
grid.line = 100,
max.freq = 105, # maximum value for the radial limits
key = list(header = "Wind Speed (m/s)",
footer = '',
labels = c('0 to 2', '2 to 4',
'4 to 6','6 or more'),
breaks = c(0,2,4,6)),
layout = c(6,1)
)
Anyone have any ideas of how to add space between the panels?
After some digging I found that this plot function utilizes trellis plots, here is a good rundown on them: https://www.stat.auckland.ac.nz/~ihaka/787/lectures-trellis.pdf
Specifically the xyplot function is used to create the trellis plot. The help documentation for ?xyplot shows that you can adjust the argument between to achieve spacing between panels. The between argument is a list containing x and y values that represent space between panels. Therefore we can adjust the above code simply by adding the argument between = list(x=0.25, y = 0.25) and can adjust x and y to our preference like this:
library(openair)
data("mydata")
windRose(mydata[1:144,], ws="ws", wd="wd",
paddle = F,
type = 'weekday',
key.header = 'Wind Speed (m/s)',
key.footer = "",
annotate = F,
angle = 30, # angle of "spokes"...sort of bins for wind direction
cols = 'jet',
key.position = 'right',
dig.lab = 2,
statistic = 'prop.count', #“prop.count” sizes bins according to the
# proportion of the frequency of measurements
fontsize = 20,
grid.line = 100,
max.freq = 105, # maximum value for the radial limits
key = list(header = "Wind Speed (m/s)",
footer = '',
labels = c('0 to 2', '2 to 4',
'4 to 6','6 or more'),
breaks = c(0,2,4,6)),
layout = c(6,1),
between = list(x=0.25, y=0.25)
)

R does not find function anno_simple() when creating a row annotation using ComplexHeatmap package

I am trying to create an heatmap with a row annotation inclusive of p-values as reported in the example in the guide for the use of the ComplexHeatmap package (https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html#simple-annotation).
I tried to reproduce the example:
library(ComplexHeatmap)
library(circlize) # colorRamp2 function
set.seed(123)
pvalue = 10^-runif(10, min = 0, max = 3)
is_sig = pvalue < 0.01
pch = rep("*", 10)
pch[!is_sig] = NA
# color mapping for -log10(pvalue)
pvalue_col_fun = colorRamp2(c(0, 2, 3), c("green", "white", "red"))
ha = HeatmapAnnotation(
pvalue = anno_simple(-log10(pvalue), col = pvalue_col_fun, pch = pch),
annotation_name_side = "left")
ht = Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)
# now we generate two legends, one for the p-value
# see how we define the legend for pvalue
lgd_pvalue = Legend(title = "p-value", col = pvalue_col_fun, at = c(0, 1, 2, 3),
labels = c("1", "0.1", "0.01", "0.001"))
# and one for the significant p-values
lgd_sig = Legend(pch = "*", type = "points", labels = "< 0.01")
# these two self-defined legends are added to the plot by `annotation_legend_list`
draw(ht, annotation_legend_list = list(lgd_pvalue, lgd_sig))
but when I am creating the annotation ha I get the error
Error in anno_simple(-log10(pvalue), col = pvalue_col_fun, pch = pch) :
could not find function "anno_simple"
likely showing a possible problem with the package.
The version of the ComplexHeatmap package I am running is 1.20.0.
The R version is 3.5.1.
Could you please help me solving this problem?
Thanks

'use.edge.length = FALSE' doesn't seem to work when using plotBranchbyTrait() in phytools

I'm trying to create a phylogeny where the branch lengths that I've coded are represented by colour rather than length. So I want the branch lengths to be equal.
Here is my code:
plotBranchbyTrait(tree.scaled, tree.scaled$edge.length, mode=c("edges"),palette="rainbow", use.edge.length = FALSE, node.depth = 2)
It's my understanding that use.edge.length = FALSE should make the branch lengths equal, and it does this if I code the tree using plot.phylo(). But the tree still shows up with the branch lengths when I use plotBranchbyTrait(). Anyone know how to get around this?
Unfortunately, optional arguments (...) are not directly passed to plot.phylo in the plotBranchbyTrait function. One non-elegant way to fix that is to modify the body directly in R to add a hard coded use.edge.length = FALSE option.
You can do this by creating a new function and modify it using body(foo)[[line_of_interest]] <- substitute(my_new_line <- that_does_something). The following example should work:
## Back up the function
plotBranchbyTrait_no_edge_length <- phytools::plotBranchbyTrait
## The line to modify:
body(plotBranchbyTrait_no_edge_length)[[34]]
# xx <- plot.phylo(tree, type = type, show.tip.label = show.tip.label,
# show.node.label = show.node.label, edge.color = colors, edge.width = edge.width,
# edge.lty = edge.lty, font = font, cex = cex, adj = adj, srt = srt,
# no.margin = no.margin, root.edge = root.edge, label.offset = label.offset,
# underscore = underscore, x.lim = x.lim, y.lim = y.lim, direction = direction,
# lab4ut = lab4ut, tip.color = tip.color, plot = plot, rotate.tree = rotate.tree,
# open.angle = open.angle, lend = 2, new = FALSE)
## Modify the line 34 by adding `use.edge.length = FALSE`
body(plotBranchbyTrait_no_edge_length)[[34]] <- substitute( xx <- plot.phylo(use.edge.length = FALSE, tree, type = type, show.tip.label = show.tip.label, show.node.label = show.node.label, edge.color = colors, edge.width = edge.width, edge.lty = edge.lty, font = font, cex = cex, adj = adj, srt = srt, no.margin = no.margin, root.edge = root.edge, label.offset = label.offset, underscore = underscore, x.lim = x.lim, y.lim = y.lim, direction = direction, lab4ut = lab4ut, tip.color = tip.color, plot = plot, rotate.tree = rotate.tree, open.angle = open.angle, lend = 2, new = FALSE) )
## Testing whether it worked
library(phytools)
tree <- pbtree(n=50)
x <- fastBM(tree)
## With use.edge.length = TRUE (default)
plotBranchbyTrait(tree, x, mode = "tips", edge.width = 4, prompt = FALSE)
## With use.edge.length = FALSE
plotBranchbyTrait_no_edge_length(tree, x, mode = "tips", edge.width = 4, prompt = FALSE)
You can find more on how to modify functions here.

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)
}

Resources