How to add notes to a ggplot
Asked Answered
P

3

11

I would like to write some notes under my ggplot. I did my data analysis in R and using now the markdown package to write my thesis. This means I can easily include variables in the markdown script. In that script I create some ggplots and was wondering if there is an easy way to write those explanations to the plot. For tables, it is pretty straight forward. %>% footnote(general="") does the trick.

Is there something like that for plots?

Pyrophosphate answered 16/7, 2019 at 7:29 Comment(5)
"I create my thesis in markdown" Does that mean you are using the rmarkdown package? Please indicate in your question (edit it). Your question seems to be about markdown and not about ggplot2. Please fix the tags.Firelock
The question is about ggplot2, not rmarkdown. He just provides an example for markdown for what he wants to accomplish. You can use annotate()Aerosol
@ClaudiuPapasteri I interpret "write some notes under my ggplot" and the reference to footnote differently.Firelock
Sorry, I was unclear. The reference to footnote, was an example of what I would like to achieve with my plot. @ClaudiuPapasteri if I'm not mistaken, with the annotate function you can write all over the plot. But I would like to write beneath it. Like a note for a figure in a paper...Pyrophosphate
It's probably possible (and preferable) to do this with markdown ...Firelock
C
20

Perhaps you are looking for something like this?

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + labs(title = "Your title", caption = "Your long reference footnote goes in here")

You need to use the caption parameter from labs() function.

Example:

enter image description here

Cenobite answered 16/7, 2019 at 8:0 Comment(2)
Thank you so much, that was exactly what I was looking for. Have a nice dayPyrophosphate
@Pyrophosphate You should accept the answer if it is what you are looking for. I also thought it might be, that's why I upvoted.Aerosol
K
3

Since you mentioned you are writing a thesis, I found the following helpful. You can insert a code chunk along the following lines:

    ```{r, fig.cap = "\\label{fig:myfigure} Here be your caption text"}
    generate_a_figue(my_data)
    ```

Then, in the main text of your markdown text you could refer to your figure as follows (Figure \ref{fig:myfigure})

Keese answered 16/7, 2019 at 9:16 Comment(0)
L
0

Also you could left align or center align your caption by adding:

    ``` + theme(plot.caption=element_text(hjust = int)

For left align : hjust = 0
For center align: hjust = 0.5
For right align : hjust = 1
Larainelarboard answered 23/7, 2021 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.