CCurtis
CCurtis

Reputation: 1932

Adding Text with Expressions to R plot

I'm plotting like crazy to finish up my MS. Adding explanatory text is easy enough using mtext or text, and adding expressions is simple too however when I need to add them both I keep having problems.

plot(1:598,xaxt='n',yaxt='n', type="l")
text(475,200,expression(paste(
"   RMSE=Root Mean Squared Error
MAD=Mean Absolute Deviance      
Average RMSE=5.78","",m^3/h,"","
","","Average MAD=4.47", "",m^3/h, sep = "")), cex=1)

My text and expressions are working but why is there are large gap after RMSE=5.78, and why doesn't the text space down after the first expression? It seems I don't understand how expression and paste are working together. Am I forced to specify separate x,y for each line?

Bonus question!! I've been looking at ggplot2 and it produces some nice looking graphs that are rendered smooth, but many of the common plotting arguments are different. I honestly found it easier to learn Matlab. I've been exporting my plots with cairo which allows me to make some high res and fairly professional quality graphs. I can pretty much do whatever I need to with the regular plotting functions. My question is does ggplot really make plotting that much faster if you are trying to make high quality publishable figures, or is its strength making quick figures looks nice? To me it seems ggplot becomes just as complicated when you want to specify a lot of details.

Upvotes: 0

Views: 1170

Answers (2)

lawyeR
lawyeR

Reputation: 7654

Ggplot is my preferred package, but I am too much a newbie to handle bquote(), do.call(), and expression().

I created this tiny counterpart of the base plot code and hope that someone can explain what to do to achieve a similar plot of the annotations:

my_text <- list( bquote( "RMSE=Root Mean Squared Error"),
                 bquote( "MAD=Mean Absolute Deviance"),
                 bquote( paste( "Average RMSE=5.78")),
                 bquote( paste( "Average MAD=4.47")))
x <- seq(1:598)
y <- seq(1:598)
df <- as.data.frame(cbind(x,y))
ggplot(df, aes(x=x, y=y)) +
  geom_point() + 
  annotate("text", label = my_text, x=100, y=100)

Upvotes: 0

koekenbakker
koekenbakker

Reputation: 3604

I think you could use the answer from this question. For your case, something like:

plot(1:598,xaxt='n',yaxt='n', type="l")

my_text <- list( bquote( "RMSE=Root Mean Squared Error" ) ,
                 bquote( "MAD=Mean Absolute Deviance" ) ,
                 bquote( paste( "Average RMSE=5.78" , m^3/h) )  ,
                 bquote( paste( "Average MAD=4.47", m^3/h ) ) )

mtext(side=1,do.call(expression, my_text), line=-1:-4, adj=0)

The line argument puts every bquote on a separate line (-1 to -4). You might need some tweaking of adj and padj to get the position right -- see ?mtext.

For the bonus question: I prefer to use regular plotting functions, as I find it more easy to control those fully, e.g. beyond the default colours and spacing. With a bit of tweaking, I also prefer the plain look of base graphics to the ggplot looks. I use regular plotting functions for all of my scientific publications, never had a problem.

Upvotes: 2

Related Questions