0

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.

CCurtis
  • 1,770
  • 3
  • 15
  • 25

2 Answers2

2

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.

Community
  • 1
  • 1
koekenbakker
  • 3,524
  • 2
  • 21
  • 30
  • Thanks. I found that using separate `text` commands and specifying the correct spacing also works, but I like your option better because then you don't have to figure out any spacing. Good to know I'm not the only person that hasn't switch over to `ggplot2`. Though I will admit it does have some cool option to make some more abstract graphs. Thanks for the help. – CCurtis Mar 16 '14 at 21:28
0

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)
lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • It appears expression doesn't work when you place it in label. See this post http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph . Looks like you need to learn some more ggplot functions to get it working. If ggplot understood more base functions them it would be much improved. I know its based on a conceptually different way of plotting but it seems like you ought to be able feed it something like `text` or `abline` and it should understand it. Maybe someone can come up with a package that translates like ggtrans(abline()) >geom_abline() – CCurtis Mar 17 '14 at 06:21