Home > Back-end >  How to pass a value into a string in ggplot?
How to pass a value into a string in ggplot?

Time:11-23

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?

CodePudding user response:

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

  • Related