Lara
Lara

Reputation: 119

How to pass a value into a string in ggplot?

Is it even possible to pass a value in ggplot?

It is possible in a console using cat

v <- 8
ggplot(df, aes(x = Category, y = Fold_Change, color = Category)) +
labs(y= "RNA expression", x = cat("p-value = 0.08831", v) )

but what about graph?

Upvotes: 0

Views: 30

Answers (1)

Karl Edwards
Karl Edwards

Reputation: 335

Use sprintf instead of cat, for example:

x = sprintf("%s %f %d", "p-value = ", 0.08831, v) )

I don't see any geom phrase in your example. I put some bogus data into a data.frame and used geom_point() to produce a plot for this example.

v <- 8
ggplot(df, aes(x = Category, y = Fold_Change, color = Category)) +
geom_point() + 
labs(y= "RNA expression", x = sprintf("%s %f %d", "p-value = ", 0.08831, v) )

example plot using sprintf

Upvotes: 1

Related Questions