How to include a \perp symbol in a ggplot2 annotation? - r

I would like to put an annotation : E \perp c using ggplot2 annotate("text", label = ...).
I searched quite thorougly on the web but only managed to get a lone symbol using annotate("text", label = "symbol('\136')", parse = T).
Does anyone have a solution ?

Plotting code from help page:
p <- ggplot(df, aes(x = gp, y = y)) +
geom_point() +
geom_point(data = ds, aes(y = mean),
colour = 'red', size = 3)
p+geom_text( aes(x="b", y=-0.4, label = "E(y)*symbol('\\136')*b" ),
parse = TRUE)
After getting this to work I was also able to get annotate(text"...) working:
p+annotate("text", 1, -0.4, label="E(y)*symbol('\\136')*b", parse=TRUE)
The tricks: to mix your quoting characters which you did but to also use plotmath syntax which I'm guessing you might not have used.
Edit: * is not a quoting character. If anything, it should be called a linking character. In plotmath syntax every "atom" or function call needs to be separated from (or "linked-to" depending on how you view it) the adjoining atoms/functions. You can do this with * (the no-space separator/linker), ~ (the spacing separator/linker), or any of the dyadic operators in the plotmath vocabulary, examples including + , -, ==, !=.

Related

What exactly is ggplot doing with the `+` operator?

I started using R recently, and have been confused with ggplot which my class is using. I'm used to the + operator just adding two outputs, but I see that in ggplot you can things such as:
ggplot(data = bechdel, aes(x = domgross_2013)) +
geom_histogram(bins = 10, color="purple", fill="white") +
labs(title = "Domestic Growth of Movies", x = " Domestic Growth")
Here we are adding two function calls together. What exactly is happening here? Is ggplot "overriding" the + operator (maybe like how you can override the == operator in dart?) in order to do something different? Or is it that the '+' operator means something different in R than I am used to with other programming languages?
I'll answer the first question. You should ask the second question in a separate posting.
R lets you override most operators. The easiest way to do it is using the "S3" object system. This is a very simple system where you attach an attribute named "class" to the object, and that affects how R processes some functions. (The ones this applies to are called "generic functions". There are other functions that don't pay any attention to the class.)
Each ggplot2 function returns an object with a class. You can use the class() function to get the class. For example, class(ggplot(data = "mtcars")) is a character vector containing c("gg", "ggplot"), and class(geom_histogram(bins = 10, color="purple", fill="white")) is the vector c("LayerInstance","Layer","ggproto","gg").
If you ask for methods("+") you'll see all the classes with methods defined for addition, and that includes "gg", so R will call that method to process the addition in the expression you used.
The + operator is part of the philosophy of ggplot2. It's inspired by The Grammar of Graphics, which is worth reading. Essentially, you keep creating new and new layers.
Try taking this one step at a time in your code and it should make sense!
one <- ggplot2::ggplot(data = mtcars) +
labs(title = "Mtcars", subtitle = "Blank Canvas")
two <- ggplot2::ggplot(data = mtcars, aes(x = mpg)) +
labs(title = "Mtcars", subtitle = "+ Aesthetic Mapping")
three <- ggplot2::ggplot(data = mtcars, aes(x = mpg, y = after_stat(count))) +
geom_histogram()
library(patchwork)
one + two + three

ggplot2 interaction use only first character

in a project i present barplots and use the interaction command to order the groups, as one is a strict subgroup of the other. I would like to not print out the whole name of the first group as this takes up a lot of space. Is there a way to restrict the word to the first character or something like that?
mtcars$name <- rownames(mtcars)
ggplot(data = mtcars, aes(x=interaction(mtcars$cyl, mtcars$name)))+
geom_bar()+
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust = 0.5))
Here for example only the #cylinders are interesting to me, I just use the car name to order them. But they take up a lot of space. Just having the first letter of the car written would be ideal. so i would like to have 8.A for example. In my original data the first variable has different length (not just 1 character as #cylinder has here)
Thanks for any answer,
Regards
You can edit the labels using regular expressions in scale_x_discrete :
library(ggplot2)
ggplot(data = mtcars, aes(x=interaction(mtcars$cyl, mtcars$name)))+
geom_bar()+
xlab('Interaction cyl vs Name') +
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust = 0.5)) +
scale_x_discrete(labels = function(x) sub('(\\..).*', '\\1', x))
Everything inside () is referred to as a capture group where we specify which part of the text we want to keep. Here, we mention that we want to keep everything until a dot (i.e \\., . is a special character in regex which needs to be escaped with \\) followed by another character (.).

Irregular bolding in plotmath expression in ggplot2

I want to be able to pass an argument from a function into a label on a ggplot graph and for that label to be in bold. Unicode won't work because many unicode symbols don't render to pdf. Plotmath expressions can be split apart in paste(), which makes them ideal for this sort of work, however I can't work out how to get the Plotmath bold(x) expression to bold the entire compiled string.
For example, this code...
y <- 5
(g <- ggplot(data.frame(x = rnorm(100,0,1)), aes(x)) +
geom_histogram(bins = 100, fill = "skyblue") +
annotate("text", x = 1.5, y = 4, label = paste0("bold(x<=", y, "^{2})"), parse = T))
...yields...
As you can see the special character (less than or equal to) has come out fine. However the problem is that the x is bolded but not the rest of the string. How do I bold the entire string in a way that still allows me to pass the outside object y into the string?

add symbol to just one axis label in R [duplicate]

I have a plot which is generated thus:
ggplot(dt.2, aes(x=AgeGroup, y=Prevalence)) +
geom_errorbar(aes(ymin=lower, ymax=upper), colour="black", width=.2) +
geom_point(size=2, colour="Red")
I control the x axis labels like this:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",">80","All")) +
This works but I need to change the ">80" label to "≥80".
However "≥80" is displayed as "=80".
How can I display the greater than or equal sign ?
An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying #mnel's example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))
Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.
You can pass an expression (including phantom(...) to fake a leading >= within
the label argument to scale_x_discrete(...)
for example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], expression(phantom(x) >=80))
See ?plotmath for more details on creating mathematical expressions and
this related SO question and answer
plot(5, ylab=expression("T ">="5"))
You can use
expression("">=80)
So your full axis label like would look like:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",expression("">=80),"All")) +
I have had trouble exporting plots when using unicode, but the expression function is more consistent.

"≥" or "≤" symbol in ggplot2 legend [duplicate]

I have a plot which is generated thus:
ggplot(dt.2, aes(x=AgeGroup, y=Prevalence)) +
geom_errorbar(aes(ymin=lower, ymax=upper), colour="black", width=.2) +
geom_point(size=2, colour="Red")
I control the x axis labels like this:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",">80","All")) +
This works but I need to change the ">80" label to "≥80".
However "≥80" is displayed as "=80".
How can I display the greater than or equal sign ?
An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying #mnel's example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))
Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.
You can pass an expression (including phantom(...) to fake a leading >= within
the label argument to scale_x_discrete(...)
for example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], expression(phantom(x) >=80))
See ?plotmath for more details on creating mathematical expressions and
this related SO question and answer
plot(5, ylab=expression("T ">="5"))
You can use
expression("">=80)
So your full axis label like would look like:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",expression("">=80),"All")) +
I have had trouble exporting plots when using unicode, but the expression function is more consistent.

Resources