How to Place +/- Plus Minus Operator in Text Annotation of Plot (Ggplot2)

How to place +/- plus minus operator in text annotation of plot (ggplot2)?

It is possible to use the unicode representation (\u00B1):

a <- ggplot()
a <- a + geom_point(aes(x=seq(0,1,0.1), y=seq(0,1,0.1)))
a <- a + annotate("text", x=0.5, y=0.3, label="myplot")
a + annotate("text", x=0.5, y=0.2, label="\u00B1")

Or you can use the ± symbol directly, by copying and pasting it from somewhere.

a + annotate("text", x=0.5, y=0.2, label="±")

Plus minus label using \u00B1 and $+-$ does not work on my annotation in ggplot

The reason is the wrong use of 1) quotes and 2) the %+-% operator - see also Ben Bolker's answer to a very related thread.

library(ggplot2)

avg1 <- paste("22.2", "\u00B1", "0.83")
## needs format x%+-%y and change the paste position
avg2 <- paste(expression(22.9 %+-% 0.67))

ggplot(data.frame()) +
## no "expression" needed, but then also no parse = TRUE
annotate(geom = "text", x = 1, y = 1, label = avg1) +
annotate(geom = "text", x = 1, y = 2, label = list(avg2), parse = TRUE)

Sample Image

Created on 2022-07-14 by the reprex package (v2.0.1)

P.S.: I'm adding the expression as a list in order to avoid a warning

How do I write *value* *plus-minus sign* *value* , along with text, with annotate of ggplot2, R?

If you set parse = TRUE the label will be displayed as described in ?plotmath. On the help page you can find a table with the avaiable features and the proper syntax.

In your case you just have to use %+-% instead of the unicode Symbol \u00B1 so that R is able to coerce it to an expression.

 l <- paste("tau^-1 ==", a[1], "%+-%", a[2], "*~s^-1")
qplot(x, y) + annotate("text", x = 1.5, y = 3.5, parse=TRUE, label = l)

How to annotate() ggplot with latex

You can use the parse argument, without expression:

p + annotate("text", x=10, y=40, label="text[subscript]", parse=TRUE)

Specify position of geom_text by keywords like top, bottom, left, right, center

geom_text wants to plot labels based on your data set. It sounds like you're looking to add a single piece of text to your plot, in which case, annotate is the better option. To force the label to appear in the same position regardless of the units in the plot, you can take advantage of Inf values:

sp <- ggplot(mpg, aes(hwy, cty, label = "sometext"))+
geom_point() +
annotate(geom = 'text', label = 'sometext', x = -Inf, y = Inf, hjust = 0, vjust = 1)
print(sp)

Sample Image

Subscripts and superscripts - or + with ggplot2 axis labels? (ionic chemical notation)

Try quoting the minus sign after the superscript operator:

ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{"-"}-N~Kg^{-1}),
y=expression(Concentration~mg~L^{-1})) +
theme(legend.title = element_text(size=12, face="bold"),
legend.text=element_text(size=12),
axis.text=element_text(size=12),
axis.title = element_text(color="black", face="bold", size=18))

I think it looks more scientifically accurate to use the %.% operator between units:

+ labs(x=expression(Production~rate~" "~mu~moles~NO[3]^{textstyle("-")}-N %.% Kg^{-1}),
y=expression(Concentration~mg~L^{-1})) +

textstyle should keep the superscript-ed text from being reduced in size. I'm also not sure why you have a " " between two tildes. You can string a whole bunch of tildes together to increase "spaces":

ggplot(df, aes(x=x, y=y))+
geom_point(size=4)+
labs(x=expression(Production~rate~~~~~~~~~~~~mu~moles~NO[3]^{textstyle("-")}-N %.% Kg^{-1}),
y=expression(Concentration~mg~L^{-1})) +
theme(legend.title = element_text(size=12, face="bold"),
legend.text=element_text(size=12),
axis.text=element_text(size=12),
axis.title = element_text(color="black", face="bold", size=18))

Sample Image

And a bonus plotmath tip: Quoting numbers is a way to get around the documented difficulty in producing italicized digits with plotmath. (Using italic(123) does not succeed, ... but italic("123") does.)

Label or annotation with subscript and variable source

If you want to "paste" two expressions together, you need to have some "operator" join them. There really isn't a paste method for expressions, but there are ways to put them together. First, obviously you could use one bquote() to put both variables together. Either

my.xlab3 <- bquote(V[DM] == .(x1)~ V[GM] == .(x2))
my.xlab3 <- bquote(list(V[DM] == .(x1), V[GM] == .(x2)))

would work. The first puts a space between them, the second puts a comma between them. But if you want to build them separately, you can combine them with another round of bquote. So the equivalent building method for the two above expressions is

my.xlab3 <- bquote(.(my.xlab1) ~ .(my.xlab2))
my.xlab3 <- bquote(list(.(my.xlab1), .(my.xlab2)))

All of those should work to set your xlab() value.

Now, if you also want to get annotate to work, you can "un-parse" your expression and then have R "re-parse" it for you and you should be all set. Observe

p + annotate("text", x=4, y=30, label=deparse(my.xlab3), parse=TRUE) 

Barplot one column based on another column from the same dataframe

Here's an approach with ggplot:

library(ggplot2)
ggplot(df, aes(x = exposed, fill = age)) +
geom_bar(position = "dodge")

Sample Image

Sample Data:

df <- structure(list(userid = c("UID 25001", "UID 25002", "UID 25003", 
"UID 25004", "UID 25005", "UID 25006", "UID 25007", "UID 25008",
"UID 25009", "UID 25010", "UID 10001", "UID 10002", "UID 10003",
"UID 10004", "UID 10005", "UID 10006", "UID 10007", "UID 10008",
"UID 10009", "UID 10010"), exposed = c("Control Group (PSA)",
"Control Group (PSA)", "Control Group (PSA)", "Control Group (PSA)",
"Control Group (PSA)", "Control Group (PSA)", "Control Group (PSA)",
"Control Group (PSA)", "Control Group (PSA)", "Control Group (PSA)",
"Test Group (Exposed)", "Test Group (Exposed)", "Test Group (Exposed)",
"Test Group (Exposed)", "Test Group (Exposed)", "Test Group (Exposed)",
"Test Group (Exposed)", "Test Group (Exposed)", "Test Group (Exposed)",
"Test Group (Exposed)"), gender = c("Male", "Male", "Female",
"Male", "Male", "Female", "Male", "Female", "Male", "Male", "Male",
"Female", "Male", "Female", "Male", "Male", "Male", "Female",
"Male", "Female"), age = c("18-25", "18-25", "51-65", "25-34",
"25-34", "18-25", "35-50", "51-65", "25-34", "51-65", "51-65",
"35-50", "35-50", "18-25", "51-65", "25-34", "51-65", "35-50",
"65+", "35-50"), income = c("$25,000 - $50,000", "$50,001 - $75,000",
"$50,001 - $75,000", "$25,000 - $50,000", "$50,001 - $75,000",
"$75,001 - $100,000", "$75,001 - $100,000", "$50,001 - $75,000",
"$50,001 - $75,000", "$50,001 - $75,000", "$50,001 - $75,000",
"$75,001 - $100,000", "Greater than $100,000", "$25,000 - $50,000",
"Greater than $100,000", "$75,001 - $100,000", "Greater than $100,000",
"$50,001 - $75,000", "$25,000 - $50,000", "$50,001 - $75,000"
), purchased = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-20L))


Related Topics



Leave a reply



Submit