Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asterisk in ggplot's annotate()

Tags:

r

ggplot2

Text can be added to a ggplot object using annotate(). However, I can't figure out how to print an asterisk, since, so far as I understand, the asterisk is used in parsing.

Here is an example. Suppose I ran a nonparametric test, and I want to print the test statistic with its significance level measured in stars. Unfortunately, this does not work:

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + 
  geom_point() + 
  annotate('text', x = 7, y = 4, label="chi^2 == 2.50***", parse=TRUE)

Error in parse(text = as.character(lab)) : <text>:1:16: unexpected '*'
1: chi^2 == 2.50***

Defining the label outside the plot object - e.g. lab <- paste0("chi^2 == 2.5","***", sep=""), then calling annotate('text', x = 7, y = 4, label=lab, parse=TRUE) - does not work either.

Is it possible to use the asterisk with annotate()?

Edit Sorry, I neglected to mention that I want the Greek letter to be compiled. Ideally the text would appear as $$chi^2 = 2.50***$$

like image 682
invictus Avatar asked Oct 24 '16 00:10

invictus


People also ask

What is an annotation in ggplot2?

Conceptually, an annotation supplies metadata for the plot: that is, it provides additional information about the data being displayed. From a practical standpoint, however, metadata is just another form of data. Because of this, the annotation tools in ggplot2 reuse the same geoms that are used to create other plots.

How do I add text to a ggplot2 plot?

To add the text, we have to indicate that we are using annotate () for adding text, and we do this by ensuring that the first input is the word 'text'. Now, ggplot2 knows that it should add a text to the plot but it still needs other information such as:

How to annotate a plot using Geom_title () function?

To annotate using this function, first, a dataframe of the values is created and then the values to geom_title () are passed with reference to the dataframe so created. This function is used to add labels i.e. text is framed in the plot, but since it does the job of adding an annotation this can be considered as one alternative.

How do you annotate a plot in Matplotlib?

To annotate a text to the plot pass the “text” as type and in the label argument, pass the text to be annotated. To annotate a shape to the plot, the type argument is passed with the required type and then coordinates are set accordingly.


1 Answers

Using a combination of double and single quotes does the trick:

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + 
  geom_point() + 
  annotate('text', x = 7, y = 4, label='chi^2 == "2.50***"', parse=TRUE)

You can use single quotes for label =, and double quotes for strings you don't want annotate to parse. Anything outside the double quotes but inside the single quotes will be parsed.

enter image description here

like image 114
acylam Avatar answered Sep 18 '22 21:09

acylam