Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot - how to add notes and description to the plot?

Tags:

plot

r

shiny

I have a long description for my plot. And I also need to put some other information under the plot as well.

But I can't figure it out how I can do it. This is what I have so far,

plot(1:10)
title(
   main = "Foo", 
   sub = 'Description \n Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. ', 
   font.sub = 1, 
   font.main = 1
)

result is not good as the description is too long and it has been cut off, also the word description is overlapping with the xlable,

enter image description here

How can I box the description under the plot?

This is what I am after,

enter image description here

  • an additional info - time period.
  • wrap the description in a grey box.
  • adjust the text to the left.

Are these possible?

like image 242
Run Avatar asked Jun 28 '15 03:06

Run


People also ask

How do you add an annotation to a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.

How do you add a description in R?

To add a variable description in R, we can use comment function and if we want to have a look at the description then structure call of the data frame will be used.

Which function is used to add text labels to a plot?

To add a text to a plot in R, the text() and mtext() R functions can be used.


2 Answers

Just a little lesson about typography: the sub argument of your function allows you to put a sub-title. To he honest such elements are not used very often. What you are trying to achieve is to put a caption (which is a very different elements). Typographic conventions say that captions are placed below the plots for graphs and images and above tables. If you use a preparation system like LaTeX or a word processors I advise you to do not put a caption "inside" the plot (say, as a part of the plot itself) by using R functions but to just put your plain plot without captions in your document and then put captions by using tools available for your editors (word etc. or LaTeX or Knitr or even Powerpoint etc. ).

Anyway, my little and boring lesson about typography does not solve your problem. Here I provide a solution with the layout function of base R and the textplot function of the gplots package but as always I bet there is a more efficient and elegant way to achieve the same goal.

library(gplots)
tex <- "Description Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \nAenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque \npenatibus et magnis dis parturient montes, ascetur ridiculus mus."
layout(matrix(c(2,1)), 0.5, 2)
par(mar=c(1,1,1,1))
textplot(tex, halign = "center")
par(mar=c(1,1,1,1))
plot(1:10)
title(main = "Foo", font.main = 1)

just to go back to boring stuff: this way of doing is very tricky and inefficient from my point of view so I advise you to use other tools as I've said previously.

Last but not least, I think that the right syntax for line breaks is \nword to break and not \n word to break

Here is how the result look:

enter image description here

like image 156
SabDeM Avatar answered Sep 28 '22 05:09

SabDeM


It can be done by using "mtext", and no space between "\n" and following words, eg. "\nLorem".

    text<- "Description \nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. \nAenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus \nmus. "
    par(mar = c(8, 4, 3, 3))
    plot(1:10, main = "Foo")
    mtext(text, side = 1, line = 6, cex = 0.8, adj = 0) 
    ## line is margin line, starting at 0 counting outwards; adj=0, left or bottom align, 1, right or top align

enter image description here

like image 21
Ying Avatar answered Sep 28 '22 03:09

Ying