Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add notes to a ggplot

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?

like image 684
severin Avatar asked Jul 16 '19 07:07

severin


People also ask

How do I add a note in ggplot2?

You can use the annotate() function to add text to plots in ggplot2. where: x, y: The (x, y) coordinates where the text should be placed. label: The text to display.

How do you annotate a graph 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.

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.


2 Answers

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

like image 190
Ankur Sinha Avatar answered Sep 21 '22 16:09

Ankur Sinha


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})

like image 24
teunbrand Avatar answered Sep 19 '22 16:09

teunbrand