Error with ggscatterplot() function from ggpubr package - r

I try to use ggscatter() function from ggpubr package with the following code
ggscatter(mtcars,
x = "hp", y = "carb",
cor.coef = TRUE, cor.method = "spearman",
xlab = "x1", ylab = "x2")
but I get the following error.
Error in parse(text = text[[i]]) : :1:16: unexpected ','
1: italic(R)~=~0,
Thanks in advance for the help.

Not sure about your question as your code is working fine.
Sample data:
data(mtcars)
Sample code:
library(ggpubr)
ggscatter(mtcars,
x = "hp", y = "carb",
cor.coef = TRUE, cor.method = "spearman",
xlab = "x1", ylab = "x2")
Plot:

I think the most likely answer to your problem is that you have set your output decimal to a comma with options(OutDec = ",")
I had the exact same problem. Here is my question with the following answer: Error in parse(text ...) unexpected comma "," when plotting ggplot with ggpubr::stat_cor and output decimal set to comma (options(OutDec = ","))
This is a bug in the package ggpubr as it cannot handle commas as
decimal seperators. For more information see here or in the comments:
MrFlick: Specifically I think it's in this line
https://github.com/kassambara/ggpubr/blob/ac5a01f586a5f0b47260fed071e79be36ef3f8ea/R/stat_cor.R#L207
. Those values should be wrapped in quotes to prevent the parsing
error. And the problem with the persistent decimal point in the
p-values comes from
https://github.com/kassambara/ggpubr/blob/ac5a01f586a5f0b47260fed071e79be36ef3f8ea/R/stat_cor.R#L219
. It doesn't look like the package really support a comma separator. –
However, there is a workaround (pointed out by #MrFlick) by passing the argument , output.type="text" in the stat_cor function e.g.:
stat_cor(show.legend = F ,label.y = c(18), method = "pearson",
output.type="text") However, there is an edge case where this does not
work, if p < 2.2e-16 (as with the example data) as the value of p is
passed as a string and not a number. Otherwise it works though.

Related

Axis title with subscript but ignore second square bracket R

I am trying to add a subscript to a ggpubr graph, which I found that I can do quite easily using parse and a set of square brackets. However, the problem is that within the axis label, I have a second set of square brackets that should be in regular type. Something like this:
library(ggpubr)
cars$speed <- log10(cars$speed)
ggscatter(cars, y = "speed", x = "dist", ylab = parse(text = "log[10](speed [μm])"))
I want the base of the logarithm (10) in subscript, but not the distance measure, [μm], which should keep it's brackets and stay in normal font size.
I have tried using paste(parse(text = "log[10]"), "(speed [μm])"), parse(text = "log[10](speed \\[μm\\]") and expression, but these have not worked for me so far.
Alternatively use expression...
ggscatter(cars, y = "speed", x = "dist", ylab = expression(log[10]*"(speed [µm])"))
Created on 2022-03-03 by the reprex package (v2.0.1)
You could do:
ggscatter(cars, y = "speed", x = "dist", ylab =
parse(text = "paste(log[10], \"(speed [μm])\")"))

How to write much less than symbol (<<) in plotmath in R?

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

Weird characters appearing in the plot legend when using DoHeatmap

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

How does the "LoadBeatRR" function from the RHRV package work?

I was trying to analyze my RR intervals using the RHRV package.
I have used:
hrv.data = CreateHRVData()
hrv.data = SetVerbose(hrv.data, TRUE)
hrv.data = LoadBeatRR(hrv.data, "dataset.txt", RecordPath = "#the path of dataset.txt here", datetime = "1/1/1900 0:0:0", verbose = NULL)
Unfortunately, when I do this I get the warning message :
"NAs introduced by coercion".
And all my following analyses do not work.
I do not see what the problem could be.
You should make sure the time, hr and rr variables are named as such: "Time", "niHR", and "RR" respectively.
A work-around would to just to manually create the beat object.
hrv.data$Beat <- readr::read_delim("txt file") %>%
dplyr::rename("Time" = time, "niHR" = hr, "RR" = rr) %>%
dplyr::select(Time, niHR, RR)
hrv.data <- RHRV::BuildNIHR(hrv.data)

How to report percentages by rows in createtableone command in tableone package?

I want to report percentage by rows using CreateTableOne from tableone package in r using the following code
vars<-c( "Group1vsGroup2", "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
CatVar<-c( "Single.Institution",
"Internal.Funding", "National.Funding",
"Industr1.Funding")
tab2 <- CreateTableOne(vars = vars, strata = "Group1vsGroup2",factorVars = CatVar,data = df,test = T);tab2<-print(tab2, margin=1,test = T, varLabels = T,quote = T,dropEqual = T)
I added "margin=1" as I saw in this website although it was for tableone command (not CreateTableOne), but I got % by columns as in the image below. Any advice will be greatly appreciated.
Here a MWE you can start with, see ?Gmisc::getDescriptionStatsBy for more details.
library(Gmisc)
# Define cyl as a factor so that getDescriptionStatsBy can use the correct Gmisc::describe*
mtcars$cyl=as.factor(mtcars$cyl)
getTable1Stats <- function(x, digits = 0,...){
getDescriptionStatsBy(x = x,
by = mtcars$am,
digits = digits,
header_count = TRUE,
...)
}
t1 <- list()
t1[["Gas"]] <- getTable1Stats(mtcars$mpg, add_total_col="last")
#hrzl_prop=FALSE will indicate that the proportions are to be interpreted in a vertical manner
t1[["Cylinder†"]] <- getTable1Stats(mtcars$cyl, hrzl_prop=TRUE, add_total_col="last")
t1[["Disp"]] <- getTable1Stats(mtcars$disp, continuous_fn=describeMedian, add_total_col="last")
mergeDesc(t1,
htmlTable_args = list(css.rgroup = "",
caption = "Basic descriptive statistics from the mtcars dataset",
tfoot = "† The weight is in 10<sup>3</sup> kg")
)
PS: This solution based on Max's vignette here. For more details on htmlTable you can see its vignette here

Resources