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])\")"))
Related
i'm trying to recreate this graph
require(car)
scatterplotMatrix(~Week+Cases+Egg.Pr+Beef.Pr+Pork.Pr+Chicken.Pr+Cereal.Pr, reg.line = lm,
smooth = TRUE, spread = FALSE, span = 0.5, id.n = 0, diagonal = 'boxplot', data = Eggs)
using the lattice package,
i've been trying to make it work with this structure, but it just comes out wrong:
splom(Eggs, panel = function(Week, Cases, Egg.Pr, Beef.Pr, Pork.Pr, Chicken.Pr, Cereal.Pr) {
panel.xyplot(Week, Cases, Egg.Pr, Beef.Pr, Pork.Pr, Chicken.Pr, Cereal.Pr)
panel.lmline(Week, Cases, Egg.Pr, Beef.Pr, Pork.Pr, Chicken.Pr, Cereal.Pr)
panel.smooth(Week, Cases, Egg.Pr, Beef.Pr, Pork.Pr, Chicken.Pr, Cereal.Pr)
}, spread = FALSE, span = 0.5, id.n = 0, diagonal = 'boxplot',
data = Eggs)
please help me out, and overexplain as much as possible
With some guessing from context, and since you asked for overexplanation:
First, you need to refine your understanding of panel functions. They are supposed to be general purpose functions describing the plotting procedure, not specific to the data at hand. With that in mind, we can rewrite your first attempt as:
myPanel <- function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.lmline(x, y, ...)
panel.loess(x, y, ...)
}
Note that panel.smooth will not work, for reasons that are too complicated to get into (but basically, it's not grid-based, as lattice requires).
With this, you should be able to do
Eggs <- Duncan[-1] # placeholder to make example reproducible
splom(Eggs, panel = myPanel)
This leaves the diagonal, for which you need another panel function. Here's one suggestion which can be fine-tuned as per your requirements:
myDiagonal <- function(x, ...) {
diag.panel.splom(x, ...) # retain default and build on it
ycenter <- quantile(x, 0.25, na.rm = TRUE)
panel.bwplot(x = x, y = rep(ycenter, length(x)),
box.width = 0.1 * diff(range(x, finite = TRUE))),
...)
}
Combining, you can now do
splom(Eggs, panel = myPanel, diag.panel = myDiagonal)
The sprinkling of ...-s are important, but I'm not going to go into why. For the rest, documentation of the corresponding functions should help you figure out how to fine tune.
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.
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 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.
I'm generating a mosaic plot with vcd::mosaic. But the text of the factors I was given are very long (cutting them is not an option, and with so many instances, introducing \n seems daunting), so there's an overlap in the texts, and I haven't been able to force the labels to go perpendicular to the axis.
This is what I'm trying:
a <- data.frame(x=sample(LETTERS[1:4],16,replace = TRUE),
y=rep(paste("very long label here at number", 1:4, paste=" "), 4))
mosaic(y ~ x, data= a, las= 2)
but this is what I get:
I've also tryed par(las= 2) and par(las= 3) but none of those is able to force them into vertical alignment (las= 2 works well with mosaicplot, though. It's like vcd::mosaic overrides las either as a given parameter or as a default-set in par. I've played also with par(mar), but the labels are long enough to fool that workaround.
What can I do to get readable labels?
########## EDIT TO ADD: ##########
I've also tried this, to no avail:
mosaic(y ~ x, data= a, labeling_list= list(gp_text= gpar(las= 2)))
and
mosaic(y ~ x, data= a, labeling_list= list(rot_labels = c(0,90,0,0)))
# Actually placed the "90" in the 4 positions
mosaic(y ~ x, data= a, labeling_list= list(rot_varnames = c(0,90,0,0)))
Finally found it! Key searching docs:
?labelings
?labeling_border
In order to rotate the labels
mosaic(y ~ x,
data= a,
labeling= labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"center")))
Try this:
+ theme(axis.text.x=element_text(angle=-25, hjust= .1))
it will rotate labels for 25 degrees (you can just copy and paste)