Related
I'm trying to write the << symbol in R but I'm not succeeding. Any tips?
plot(expression(alpha<1))
plot(expression(alpha<<1))
Error: unexpected input in "plot(expression(alpha<<"
Using unicode
plot(1, 1, xlab = bquote(alpha~"\u226A"~1))
This is the closest I am so far, how does this work for you?
plot(
x = 1:10,
y = 1:10,
main = expression(paste(alpha, "<<", 1))
)
I was using Seurat to analyse single cell RNA-seq data and I managed to draw a heatmap plot with DoHeatmap() after clustering and marker selection, but got a bunch of random characters appearing in the legend. They are random characters as they will change every time you run the code. I was worrying over it's something related to my own dataset, so I then tried the test Seurat object 'ifnb' but still got the same issue (see the red oval in the example plot).
example plot
I also tried importing the Seurat object in R in the terminal (via readRDS) and ran the plotting function, but got the same issue there, so it's not a Rstudio thing.
Here are the codes I ran:
'''
library(Seurat)
library(SeuratData)
library(patchwork)
InstallData("ifnb")
LoadData("ifnb")
ifnb.list <- SplitObject(ifnb, split.by = "stim")
ifnb.list <- lapply(X = ifnb.list, FUN = function(x) {
x <- NormalizeData(x)
x <- FindVariableFeatures(x, selection.method = "vst", nfeatures = 2000)
})
features <- SelectIntegrationFeatures(object.list = ifnb.list)
immune.anchors <- FindIntegrationAnchors(object.list = ifnb.list, anchor.features = features)
immune.combined <- IntegrateData(anchorset = immune.anchors)
immune.combined <- ScaleData(immune.combined, verbose = FALSE)
immune.combined <- RunPCA(immune.combined, npcs = 30, verbose = FALSE)
immune.combined <- RunUMAP(immune.combined, reduction = "pca", dims = 1:30)
immune.combined <- FindNeighbors(immune.combined, reduction = "pca", dims = 1:30)
immune.combined <- FindClusters(immune.combined, resolution = 0.5)
DefaultAssay(immune.combined) <- 'RNA'
immune_markers <- FindAllMarkers(immune.combined, latent.vars = "stim", test.use = "MAST", assay = 'RNA')
immune_markers %>%
group_by(cluster) %>%
top_n(n = 10, wt = avg_log2FC) -> top10_immune
DoHeatmap(immune.combined, slot = 'data',features = top10_immune$gene, group.by = 'stim', assay = 'RNA')
'''
Does anyone have any idea how to solve this issue other than reinstalling everything?
I have been having the same issue myself and while I have solved it by not needing the legend, I think you could use this approach and use a similar solution:
DoHeatmap(immune.combined, slot = 'data',features = top10_immune$gene, group.by = 'stim', assay = 'RNA') +
scale_color_manual(
values = my_colors,
limits = c('CTRL', 'STIM'))
Let me know if this works! It doesn't solve the source of the odd text values but it does the job! If you haven't already, I would recommend creating a forum question on the Seurat forums to see where these characters are coming from!
When I use seurat4.0, I met the same problem.
While I loaded 4.1, it disappeared
I'm a beginner in R and I'm trying to use the package ClonEvol, however the documentation on the github webpage is very limited. So for now I'm using their example code and trying to adapt it to my data called ce.
ce <- data.frame(
cluster = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7),
gene = c("geneA","geneB","geneC","geneD","geneA","geneB","geneC","geneD","geneA","geneB","geneC","geneD","geneA","geneB","geneC",
"geneD","geneA","geneB","geneC","geneD","geneA","geneB","geneC","geneD","geneA","geneB","geneC","geneD"),
prim.vaf = c(0.5,0,0,0,0.5,0.5,0,0,1,0.5,0,0,1,0.5,0,0.5,0.5,0.5,0,0.5,0.5,0.5,0,1,0.5,0.5,0.5,0)
)
cluster <- ce$cluster
gene <- ce$gene
prim.vaf <- ce$prim.vaf
x <- ce
vaf.col.names <- grep('prim.vaf', colnames(x), value=T)
sample.names <- gsub('prim.vaf', '', vaf.col.names)
x[, sample.names] <- x[, vaf.col.names]
vaf.col.names <- sample.names
sample.groups <- c('P', 'R');
names(sample.groups) <- vaf.col.names
x <- x[order(x$cluster),]
pdf('box.pdf', width = 3, height = 5, useDingbats = FALSE, title='')
pp <- variant.box.plot(x,
cluster.col.name = ce$cluster,
show.cluster.size = FALSE,
cluster.size.text.color = 'blue',
vaf.col.names = vaf.col.names,
vaf.limits = 70,
sample.title.size = 20,
violin = FALSE,
box = FALSE,
jitter = TRUE,
jitter.shape = 1,
jitter.color = clone.colors,
jitter.size = 3,
jitter.alpha = 1,
jitter.center.method = 'median',
jitter.center.size = 1,
jitter.center.color = 'darkgray',
jitter.center.display.value = 'none',
highlight = 'is.driver',
highlight.note.col.name = 'gene',
highlight.note.size = 2,
highlight.shape =16,
order.by.total.vaf = FALSE
)
dev.off()
However, I get the following error :
Error in .subset2(x, i, exact = exact) : recursive indexing failed at level 2
And if I delete cluster.col.name=ce$cluster and vaf.col.names=vaf.col.names, the error becomes the following :
Error in .subset2(x, i, exact = exact) : attempt to select less than one
element in get1index
Has someone any idea of what went wrong ?
I met this error message today. I know little about the R package in this question, but I think I can show what the error message means here. And maybe it is useful for you to find out the problem.
The error occurs when we use NULL as an index to subset a list.
Here are the expressions invoking this error message:
any.list <- list(1, 2, 3)
# If single brackets, no error occurs:
any.list[NULL]
## list()
# If double brackets, the error occurs:
any.list[[NULL]]
## Error in any.list[[NULL]] :
## attempt to select less than one element in get1index
The list above can be any list, or even a vector.
a.vector <- c(1, 2, 3)
a.vector[[NULL]]
## Error in a.vector[[NULL]] :
## attempt to select less than one element in get1index
Here is a similar error message:
any.list[[0]]
## Error in any.list[[0]] :
## attempt to select less than one element in get1index <real>
END
I want to include math symbols in the panel titles for this stratigraphic plot:
library(analogue)
data(V12.122)
Depths <- as.numeric(rownames(V12.122))
names(V12.122)
(plt <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
data = V12.122,
type = c("h","l","g"),
zones = 400))
plt
For example, I want to have this text in place of "O.univ" etc.:
I used this code to make that text:
plot(1, type="n", axes=FALSE, ann=FALSE)
title(line = -1, main = expression(phantom()^14*C~years~BP))
title(line = -3, main = expression(delta^18*O))
title(line = -5, main = expression(paste("TP ", mu,"g l"^-1)))
title(line = -10, main = expression("very long title \n with \n line breaks"))
But if I try to update the colnames of the data frame passed to Stratiplot, the code is not parsed, and we do not get the correct text formatting:
V12.122 <- V12.122[, 1:4]
names(V12.122)[1] <- expression(phantom()^14*C~years~BP)
names(V12.122)[2] <- expression(delta^18*O)
names(V12.122)[3] <- expression(paste("TP ", mu,"g l"^-1))
(plt <- Stratiplot(Depths ~ .,
data = V12.122,
type = c("h","l","g"),
zones = 400))
plt
How can I get Stratiplot to parse the expressions in the colnames and format them correctly in the plot?
I've tried looking through str(plt) to see where the panel titles are stored, but no success:
text <- expression(phantom()^14*C~years~BP)
plt$condlevels$ind[1] <- text
names(plt$packet.sizes)[1] <- text
names(plt$par.settings$layout.widths$panel)[1] <- text
You can't actually do this in the current release of analogue; the function is doing too much messing around with data for the expressions to remain unevaluated prior to plotting. I could probably figure this out to allow expressions as the names of the data argument object, but it is easier to just allow users to pass a vector of labels that they want for the variables.
This is now implemented in the development version of the package on github, and I'll push this to CRAN early next week.
This change implements a new argument labelValues which takes a vector of labels for use in labelling the top axis. This can be a vector of expressions.
Here is an illustration of the usage:
library("analogue")
set.seed(1)
df <- setNames(data.frame(matrix(rnorm(200 * 3), ncol = 3)),
c("d13C", "d15N", "d18O"))
df <- transform(df, Age = 1:200)
exprs <- expression(delta^{13}*C, # label for 1st variable
delta^{15}*N, # label for 2nd variable
delta^{18}*O) # label for 3rd variable
Stratiplot(Age ~ ., data = df, labelValues = exprs, varTypes = "absolute", type = "h")
which produces
Note that this is just a first pass; I'm pretty sure I haven't accounted for any reordering that goes on with sort and svar etc. if they are used.
Never used lattice plots, but I thought a chance to learn something should be worth while. Took too long to figure out.
text <- "c( expression(phantom()^14*C~years~BP),expression(delta^18*O))"
strip = strip.custom(factor.levels=eval(parse(text=text)))
plt <- Stratiplot(Depths ~ .,
data = V12.122[, 1:4],
type = c("h","l","g"),
zones = 400,
strip = strip)
Hope this gets you started.
Here is my code
slidingwindowplotATGC = function(windowsize, inputseq)
{
starts = seq(1, length(inputseq)-windowsize, by = windowsize)
n = length(starts)
chunkGs = numeric(n)
chunkAs = numeric(n)
chunkTs = numeric(n)
chunkCs = numeric(n)
for (i in 1:n) {
chunk = windowsize[starts[i]:(starts[i]+9999)]
chunkG = sum("g" == chunk)/length(chunk)
chunkA = sum("a" == chunk)/length(chunk)
chunkT = sum("t" == chunk)/length(chunk)
chunkC = sum("c" == chunk)/length(chunk)
chunkGs[i] = chunkG
chunkAs[i] = chunkA
chunkTs[i] = chunkT
chunkCs[i] = chunkC
}
plot(starts,chunkGs,type="b",ylim=c(min(min(chunkAs),min(chunkTs),min(chunkCs),min(chunkGs)),max(max(chunkAs),max(chunkTs),max(chunkCs),max(chunkGs))),col = "red")
points(starts,chunkTs,col = "blue")
points(starts,chunkAs,col = "green")
points(starts,chunkCs)
}
Im getting the following error message,
Error in seq.default(1, length(inputseq) - windowsize, by = windowsize) :
wrong sign in 'by' argument
which I never got before when running codes of this sort, infact I re ran old code that worked perfectly before, except this time Im getting this error message which doesn't seem to make any sense at all! I need help with this before I go completely insane... Maybe Im just bad at this program, but it seems to me that it has a mind of its own... I was also getting an error message before regarding the ylim function, stating that it needed to be a finite value, which is what I was giving it? HELP!!!
Change
starts = seq(1, length(inputseq)-windowsize, by = windowsize)
to
starts = seq(1, nchar(inputseq)-windowsize, by = windowsize)
assuming you're using a character vector as inputseq, such as
slidingwindowplotATGC(3, "ATAGACGATACGATACCCCGAGGGTAGGTA")
ETA: Aside from that difference, there are some very serious issues with how you are using character vectors. For example:
windowsize[starts[i]:(starts[i]+9999)]
Why does it look like you are selecting from windowsize, which is just an integer of your window size? Were you trying to select from inputseq?
Even if you were selecting from inputseq, the way to do that is substr(inputseq, start, stop)
Where does the starts[i]+9999 come from? Do you mean starts[i]+windowsize?
You should start over and carefully consider what you are trying to do, and learn the right tools to do it within R.
ETA: Here is a proposed rewrite of what you're trying to do (you'll need to install the zoo package first):
library(zoo)
slidingwindowplotATGC = function(windowsize, inputseq)
{
print(nchar(inputseq)-windowsize)
s = strsplit(inputseq, "")[[1]]
starts = seq(1, nchar(inputseq)-windowsize, by = windowsize)
n = length(starts)
letters = c("a", "c", "g", "t")
colors = c("green", "black", "red", "blue")
counts = t(sapply(letters, function(l) rollapply(s, windowsize, function(x) mean(x == l))))
plot(counts[1, ], type="l", col=colors[1])
for (i in 2:4) {
points(counts[i, ], type="l", col=colors[i])
}
print(counts)
}
slidingwindowplotATGC(10, "aagaaaagatcaaagaccagccgccccaccccccagagccccccc")
This should get you most of the way there. After that, you're on your own ;-)
A further condensation. You need to specify windowsize (width of the window) and by (periodicity of sampling) separately, although I think you wanted them the same (i.e. chop the sequence into exclusive chunks) -- if you want a sliding window you could use by=1.
The error you're seeing above is most likely occurring because for some reason windowsize is greater than nchar(inputseq).
slidingwindowplotATGC = function(windowsize, by, inputseq) {
s = strsplit(inputseq, "")[[1]]
colors = c("green", "black", "red", "blue")
counts = rollapply(factor(s), width=windowsize, by=by,table)
matplot(counts,type="l", lty=1,col=colors)
counts
}
itest <- "aagaaaagatcaaagaccagccgccccaccccccagagccccccc"
slidingwindowplotATGC(10, itest)
You should also check Bioconductor -- it's extremely likely that there's efficient code in there somewhere for doing this sort of summary.