How to pass a string argument to expression function in ggplot2? - r

I'm writing a wrapper function around ggplot2 and having difficulty with one of the string arguments passed. Here's the sample code
myPlot <- function(tim, labx){
ggplot(subset(dat,TIME=tim), aes(x=WT, y=Var))+
geom_point(size=2)+
facet_wrap(~Dose)+
scale_x-continuous(expression(bold("Predicted"~labx~"Concentration ("*mu*"g/mL)")))
}
When I say myplot(100, "Week3"), my x-axis label is showing as "Predicted labx Concentration (µg/mL)" instead of "Predicted Week3 Concentration (µg/mL)". How do I fix this?

One solution is to use bquote() instead of expression(), and use .() inside of bquote to evaluate character (string) variables.
Below is a fully reproducible example of the issue.
library(ggplot2)
labx = "Week3"
p = ggplot(data.frame(x=1:5, y=1:5), aes(x, y)) +
geom_point() +
xlab(bquote(bold(Predicted~.(labx)~Concentration~(mu*g/mL))))
p

Related

True negative sign in continuous y-axis scale

Owing to the journal's formatting requirement, I need to use a true negative sign (UTF-16: 2212) for my graphs. This is similar to this post, except that my existing plots are based on ggplot. Rather than reconfiguring them back to base R plot, I am hoping to find a ggplot2-native solution.
The current ggplot-based questions here unfortunately deal with text annotations, character axis ticks, or headings instead of continuous axis ticks. Meanwhile, the closest idea I have for the scale_y_continuous function is to use the label variable in the function or simply use break and manually tune all the relevant axes, but I am still figuring it out.
The manual way for now:
ggplot(test, aes(x = x, y = y)) +
geom_raster(aes(fill = value)) +
scale_x_continuous(breaks = -3:3, labels = c("\U2212\U0033","\U2212\U0032","\U2212\U0031",0,1,2,3))
This produces a plot with the default hyphen-minus sign on the y-axis and the true minus sign on the x-axis. However, the number has to be coded in UTF format, else it could be concatenated by the UTF reader into another UTF character (i.e. "\U22123" is a Chinese character instead of -3.)
May I ask if there is a more elegant and/or dynamic solution to this?
You can write your own labeller function.
library(ggplot2)
label_trueminus <- function(x){
ifelse(sign(x) == -1, paste0("\u2212", abs(x)), x)
}
ggplot(mtcars, aes(mpg-20, disp)) +
geom_point() +
scale_x_continuous(labels = label_trueminus)
Created on 2022-01-25 by the reprex package (v2.0.1)
or even shorter:
library(tidyverse)
label_parse <- function(breaks) {
parse(text = breaks)
}
# sample data
tibble(
a = -5:5,
b = rnorm(length(a))
) %>%
# sample plotting
ggplot(aes(a,b)) +
geom_point() +
# here's the magic
scale_x_continuous(labels = label_parse)

ggplot2: How to use variables inside expression for labeling axis? [duplicate]

I'm writing a wrapper function around ggplot2 and having difficulty with one of the string arguments passed. Here's the sample code
myPlot <- function(tim, labx){
ggplot(subset(dat,TIME=tim), aes(x=WT, y=Var))+
geom_point(size=2)+
facet_wrap(~Dose)+
scale_x-continuous(expression(bold("Predicted"~labx~"Concentration ("*mu*"g/mL)")))
}
When I say myplot(100, "Week3"), my x-axis label is showing as "Predicted labx Concentration (µg/mL)" instead of "Predicted Week3 Concentration (µg/mL)". How do I fix this?
One solution is to use bquote() instead of expression(), and use .() inside of bquote to evaluate character (string) variables.
Below is a fully reproducible example of the issue.
library(ggplot2)
labx = "Week3"
p = ggplot(data.frame(x=1:5, y=1:5), aes(x, y)) +
geom_point() +
xlab(bquote(bold(Predicted~.(labx)~Concentration~(mu*g/mL))))
p

ordering function seq() in R with the order of input value

My apologize for my bad english i'm a student from france.
I have a little problem with a function in R, indeed i have a dataframe like that :
https://imgur.com/G5ToQrL
With this code :
testtransect2$TOTAL<-testtransect2$TOTAL*-1
plot(testtransect2$DECA,testtransect2$TOTAL,asp = 1)
xl <- seq(min(testtransect2$DECA),max(testtransect2$DECA), (max(testtransect2$DECA)-min(testtransect2$DECA))/1000)
lines(xl, predict(loess(testtransect2$TOTAL~testtransect2$DECA,span = 0.25), newdata=xl))
I want to create a plot with a smooth line which pass through all the point in the order of the dataframe but when i want put my line with my value xl and predict my plot is not like i want :
https://imgur.com/cSlhNtV
I link you a plot where you can see what i want :
https://imgur.com/mnVgvQ7
i think it's a problem of order in my xl value but i can't do it, if you have any solution
Thanks for give it to me
You can use ggplot
Storing your dataframe in df
df <- data.frame(DECA=c(0,10,15,-23,15,40,90,140,190,250,310,370,420),
TOTAL=c(0,-9,-15,-31.5,-48,-50,-44,-24,-17,-10,-6,-5,0))
You are interested in geom_point and geom_line. You can specify df$DECA and df$TOTAL in aes like this:
library(ggplot)
ggplot(df, aes(x=DECA, y=TOTAL)) +
geom_line() + geom_point()
Yielding
The "but when i want put my line with my value xl and predict my plot is not like i want" part is unfortunately unclear to me, please rephrase if this solution does not work for you.
Updated
There are other smooth_lines that may be added, eg. geom_smooth. Is this what you request?
ggplot(df, aes(x=DECA, y=TOTAL)) +
geom_line() + geom_point() +
geom_smooth(se=F, method = lm, col="red") + #linear method
geom_smooth(se=F, col="green") # loess method

Convert string parameter in function to variable name in R

I have the following function:
myfunction <- function(arg1){
arg1 <- m$value
coverage_plot <- ggplot(data=m, aes(x=Time, y=arg1, group=Technique, color=Technique)) + geom_line()
I want to pass a string argument (e.g., "Age") and assign the m$value to Age. Currently, in the plot, the y-axis label is shown as arg1, which should be Age.
I thought about converting the string argument to a variable name by using as.name function:
var <- as.name(arg1)
var <- m$value
coverage_plot <- ggplot(data=m, aes(x=Time, y=var, group=Technique, color=Technique)) + geom_line()
I ended up by having the same issue, which is var is shown in the plot rather than Age.
I also tried to use the assign function as assign(arg1,m$value) but didn't work as well.
So the question is how to convert the passed argument to a variable that can be shown in the y-axis label?
You could use aes_string function and provide it like this:
aes_string(x="Time", y=var, group="Technique", color="Technique")
I assume that "Time" and "Technique" are names of columns in m, not variables.
The answer to my question was very simple. The solution is to change the y axis label to the argument string as:
myfunction <- function(arg1){
yValues <- m$value
the_plot <- ggplot(data=m, aes(x=Time, y=yValues, group=Technique, color=Technique)) + geom_line() + ylab(arg1)

ggplot facet_grid label superscript

I am having trouble with putting subscript in facet_grid label. Here is
an example of the work I have been trying to do.
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=c('N1P1 var','N2P1 var')),
value=rnorm(40))
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species)
Now I want to make by vertical facet label as 'N[1]P[1] var' and so on,
where the numbers in the squared bracket means subscript.
I have consulted some helps in this platform regarding this, but none helped me. I have used expression, bquote as suggested, but nothing worked!
You need to do 2 things:
first, make your labels as plotmath expressions
variable_labels <-
c(expression(paste(N[1],P[1]~var)), expression(paste(N[2],P[1]~var)))
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=variable_labels),
value=rnorm(40))
And then change the default labeller function in facet_grid to "label_parsed"
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species, labeller = "label_parsed")

Resources