Discussion:
geom_text - Math expression label annotation - color, parsing, and subgraph labeling problems
m***@gmail.com
2018-08-13 09:28:24 UTC
Permalink
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.



[image: example.png] <about:invalid#zClosurez>
--
--
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 ***@googlegroups.com
To unsubscribe: email ggplot2+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brandon Hurr
2018-08-13 18:24:19 UTC
Permalink
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:
https://stackoverflow.com/questions/35880053/superscripting-in-ggplot2-using-plotmath
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)

[image: 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
[image: Screen Shot 2018-08-13 at 11.22.11 AM.png]

HTH,
B
Post by m***@gmail.com
Hello,
Three issues actually I'm just summing them up down here: !
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
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 ?
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.
[image: example.png]
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
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
For more options, visit https://groups.google.com/d/optout.
--
--
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 ***@googlegroups.com
To unsubscribe: email ggplot2+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
m***@gmail.com
2018-08-13 20:59:57 UTC
Permalink
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.



From: Brandon Hurr <***@gmail.com>
Sent: Monday, August 13, 2018 8:24 PM
To: ***@gmail.com
Cc: ggplot2 <***@googlegroups.com>
Subject: Re: geom_text - Math expression label annotation - color, parsing, and subgraph labeling problems



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:

https://stackoverflow.com/questions/35880053/superscripting-in-ggplot2-using-plotmath

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)







#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





HTH,

B



On Mon, Aug 13, 2018 at 2:28 AM <***@gmail.com <mailto:***@gmail.com> > wrote:

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.
--
--
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 ***@googlegroups.com <mailto:***@googlegroups.com>
To unsubscribe: email ggplot2+***@googlegroups.com <mailto:ggplot2%***@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+***@googlegroups.com <mailto:ggplot2+***@googlegroups.com> .
For more options, visit https://groups.google.com/d/optout.
--
--
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 ***@googlegroups.com
To unsubscribe: email ggplot2+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...