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

78 views
Skip to first unread message

marios...@gmail.com

unread,
Aug 13, 2018, 5:28:25 AM8/13/18
to ggplot2

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 a bitch 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. ( For example like a graph I attach )


Thanks in advance for your help !


M.



example.png

Brandon Hurr

unread,
Aug 13, 2018, 2:24:48 PM8/13/18
to marios...@gmail.com, ggplot2
Marios, 

#1
You're using geom_text() which inherits the aes() from ggplot() so you're seeing ggplot's best attempt at making what you're asking it to do work. 
You are absolutely seeing overplotting (layers of the same text) because when you give it a single label, it replicates that label for the entire length of your dataset and prints that label to your plot many times. 

The way around this is to use annotate(), which is meant to be used for this exact example. 

#2
See here:
You can't put spaces in your label. You need to use ~ as a space character and ~~ if you want extra space. 

The following should address your issues with #1 and #2. 

library(tidyverse)
library(viridis)

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() +
    annotate(geom="text", label="Alpha[qq]%~~%10^10~A", x = Inf, y = -Inf, hjust = 1, vjust = 0, parse = TRUE, colour = "black", size = 7) +
    annotate(geom="text", label="(d)", x = -Inf, y = Inf, hjust = 0, vjust = 1.5, parse = FALSE, colour = "black", size = 7) +
    scale_color_viridis(option="B", discrete = TRUE, begin = 0.0, end = 0.85, direction = 1)

Screen Shot 2018-08-13 at 11.23.21 AM.png

#3... This is harder, but some newer tools will make this easier. Assuming you have updated R and installed the latest ggplot2 and patchwork from github:

library(tidyverse
library(viridis)
library(patchwork)

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

p <- ggplot(reprex.data, aes(x = x, y = y, colour = color, shape = color )) + 
    geom_point() +
    annotate(geom="text", label="Alpha[qq]%~~%10^10~A", x = Inf, y = -Inf, hjust = 1, vjust = 0, parse = TRUE, colour = "black", size = 7) +
    annotate(geom="text", label="(d)", x = -Inf, y = Inf, hjust = 0, vjust = 1.5, parse = FALSE, colour = "black", size = 7) +
    scale_color_viridis(option="B", discrete = TRUE, begin = 0.0, end = 0.85, direction = 1)

p1 <- p + labs(tag = "(a)") + theme(plot.tag.position = "bottom")
p2 <- p + labs(tag = "(b)") + theme(plot.tag.position = "bottom")
p3 <- p + labs(tag = "(c)") + theme(plot.tag.position = "bottom")

p1 + p2 + p3
Screen Shot 2018-08-13 at 11.22.11 AM.png

HTH,
B

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

marios...@gmail.com

unread,
Aug 13, 2018, 5:00:14 PM8/13/18
to Brandon Hurr, ggplot2

Thank you Brandon !

By the way, I just found out that ggplot2 v 3.0.0 has a new feature called tag allowing to finally tag easily and generically the subplots of a plot !

 

  labs(tag = "(d)")

 

the position handlers are inside the theme pack and inherit style from graph title !

Thanks again,
M.

image001.png
image002.png
image003.jpg
Reply all
Reply to author
Forward
0 new messages