Geom_text - Math expression label annotation - color, parsing, and subgraph labeling problems

Hello,

Three issues actually I'm just summing them up down here: !

#1 :

I have a dataset where I'm incrementing colour by a variable in the aes. I then annotate an expression on the graph which i want to force to plain old simple black ( If I don't force it my geom_text inherits the coloring of the aes. I'm forced to inherit the aes as when I tried to define a custom aes for that layer I have an error saying that the color variabile is not recognized)

Here's an example


reprex.data= data.frame(x= c(4.5, 3,3,3.5,3.5,4,4,0), y= c(4.5, 3,3,3.5,3.5,4,4,0), color= c("A","A","A","A","B","B","A","A"))

{windows(width = 8, height = 5)

  ggplot(reprex.data, aes(x=x,y=y, colour= color, shape = color )) + 

    geom_point() +

    geom_text(aes(label="Alpha[qq]%~~%10^10", x=1.5, y=-3), parse=T, colour= "black", size = 7)+

    scale_color_viridis(option="B", discrete = TRUE, begin = 0.0, end = 0.85, direction = 1)+

    geom_text(aes(label="(d)", x=0, y=5) , parse=T, colour= "black", size = 7)

}

If you look closely to the annotation text you'll notice that there seem to be 2 colours overlapping or 2 layers overlapping ? Is this a double print ?
At least it's the case in my R 3.4.3

#2 :

geom_text:

This works:

geom_text(aes(label="Alpha[qq]%~~%10^10", x=1.5, y=-3), parse=T, colour= "black", size = 7)

but as soon as I write an expression like

`"Alpha[qq]%~~%10^10 A"`

It fails. I need to be able to write the unit after my number. Any ideas ?

#3 :

Since I'm in the field of applied physics and engineering we often tend to have numerous sub plots
which you want to numerotate likr (a) (b) (c) etc.

And that's not at all straightforward in ggplot2

I'm forced to used annotate or geom_text as above, but anyone knows of a way that I can have it outside of my graph space ?

Ideally I want the annotation to be created always in the bottom left of my graph, aligned to the x and y axis titles.
I tried using caption pushing the hjust and vjust handles but you need to use negative values and it's not very reproducible from graph to graph.....
I'd like to try and make a template if possible.

Thanks in advance for your help !

M.

OK, so first thing's first: getting your code working, and into a proper reprex (short for reproducible example).

If you're new to reprex, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful. For pointers specific to the community site, check out the reprex FAQ.

Note that in order for this to work, I had to load the libraries being used (I could have done library(ggplot2), I just happened not to).

I changed your scale_color_viridis with the discrete = TRUE argument, to scale_color_viridis_d() (the d stands for discrete). You can read more about new functionality for scales and guides in ggplot2 3.0.0 here:

library(tidyverse)
reprex.data <- data.frame(x = c(4.5, 3, 3, 3.5, 3.5, 4, 4, 0), y = c(4.5, 3, 3, 3.5, 3.5, 4, 4, 0), color = c("A", "A", "A", "A", "B", "B", "A", "A"))

ggplot(reprex.data, aes(x = x, y = y, colour = color, shape = color)) +
  geom_point() +
  geom_text(aes(label = "Alpha[qq]%~~%10^10", x = 1.5, y = -3), parse = T, colour = "black", size = 7) +
  scale_color_viridis_d(option = "B", begin = 0.0, end = 0.85, direction = 1) +
  geom_text(aes(label = "(d)", x = 0, y = 5), parse = T, colour = "black", size = 7)

Created on 2018-08-13 by the reprex package (v0.2.0.9000).

Also in ggplot2 3.0.0 is a tag option:

In addition to title, subtitle, and caption, a new tag label has been added, for identifying plots. Add a tag with labs(tag = "A") , style it with the plot.tag theme element, and control position with the plot.tag.position theme.

I didn't add any styling, but here's your example with (d) passed as a tag:

library(tidyverse)
reprex.data <- data.frame(x = c(4.5, 3, 3, 3.5, 3.5, 4, 4, 0), y = c(4.5, 3, 3, 3.5, 3.5, 4, 4, 0), color = c("A", "A", "A", "A", "B", "B", "A", "A"))

ggplot(reprex.data, aes(x = x, y = y, colour = color, shape = color)) +
  geom_point() +
  geom_text(aes(label = "Alpha[qq]%~~%10^10", x = 1.5, y = -3), parse = T, colour = "black", size = 7) +
  scale_color_viridis_d(option = "B", begin = 0.0, end = 0.85, direction = 1) +
  labs(tag = "(d)")

Created on 2018-08-13 by the reprex package (v0.2.0.9000).

I don't have a ready-made solution off the top of my head for the equation at the bottom. Someone else might, though. You could certainly make a helper function, though.

4 Likes

If adding a single label, it is generally most straightforward to use annotate() since geom_text() will repeat the label based on the length of the plotting dataset.

From the documentation of geom_text():

geom_text and geom_label both add a label for each row in the data, even if coordinates x, y are set to single values in the call to geom_label or geom_text . To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...)

So you could do

annotate(geom = "text", label = "Alpha[qq]%~~%10^10", x = 1.5, y = -3, 
               parse = TRUE, colour = "black", size = 7)

Alternatively you could pass geom_text() an empty data.frame and use inherit.aes = FALSE:

 geom_text(data = data.frame(), aes(label = "Alpha[qq]%~~%10^10", x = 1.5, y = -3), 
               parse = TRUE, colour = "black", size = 7, inherit.aes = FALSE)

In terms of adding your units, I wasn't sure I understood exactly. Do you want something like
label = "Alpha[qq]%~~%10^10~units"?

3 Likes