ggplot2: How to use variables inside expression for labeling axis? [duplicate] - 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)

How to use position_jitter_tern() in ggtern() in R?

I am creating a simple ternary plot.
ggtern(data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7)),aes(x,y,z)) + geom_point()
How can I jitter the point so that the plot will display two points?
I tried using position_jitter_tern like so: but it isn't changing anything.
ggtern(data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7)),aes(x,y,z, position_jitter_tern(0.1,0.1,0.1))) + geom_point()
Image can be seen here
You need to use the "position" option inside the geom_point function.
library(ggtern)
df <- data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7))
ggtern(data=df, aes(x,y,z) ) +
geom_point(position= position_jitter_tern(x=0.1, y=0.1, z=0.02))
You can apply base jitter function to the dataframe.
library(ggtern)
library(ggplot2)
data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7))
data[] <- lapply(data, jitter, 3)
ggtern(data,aes(x,y,z)) + geom_point()

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

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

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

ggplot geom_text annotation with variable label

Is it possible to annotate a ggplot figure with a "text" element indicating a feature of the data (variable)?
library(ggplot2)
library(datasets)
my.mean <- mean(mtcars$mpg, na.rm=T)
my.mean <- as.name(my.mean)
gplot <- ggplot(mtcars, aes(mpg))+geom_histogram()
gplot <- gplot + geom_text(aes_string(label=my.mean, y=5), size=3)
This produces something on the plot that looks like a succession of numbers. Any ideas how to resolve this?
Edit: this question is different since I am not trying to annotate each histogram bin with a value. The objective is to add one single text element to the plot.
If I understood you right, you want to add a text to your plot which is defined by another dataset, i.e. a dataset which was not given as argument to ggplot().
Solution: Pass this dataset directly to your geom_text function using data=... to use it.
library(ggplot2) library(datasets)
my.mean <- mean(mtcars$mpg, na.rm=T)
ggplot(mtcars, aes(mpg)) +
geom_histogram() +
geom_text(data=data.frame(my.mean=my.mean), aes(y=5, x=my.mean, label=my.mean), size=3)
it should work like this:
gplot <- gplot + geom_text(aes(15, 5, label="some random text"))
gplot
with the numbers you can specify the location within your grid.

Resources