Reputation: 404
I would like to use expression
in combination with a sprintf
-result for labeling in ggplot2
or plot
.
So I tried (meaningless example, just for illustrating the problem):
require(ggplot2)
test <- 2
ggplot(data = iris, aes(x = Sepal.Length)) +
geom_bar() +
scale_x_continuous(name = expression(""<=sprintf(paste0("%.", test, "f"), pi)))
Obviously, that doesn't work (just evaluating <=
and pi
), even though both separate elements does. Is there a possibility to rewrite the expression, so it will work with the combination? I already tried to include ~
or eval(parse(text = ...))
but these do not work either. Last: it is essential to integrate the test
-object into sprintf
for me.
Thanks in advance!
EDIT So here is a better example. Thought I made the other one as minimal as possible, but obviously than it wasn't specific enough. Sorry for that. Hopefully, it is possible to understand my question know. Thank you!
require(ggplot2)
require(scales)
# data
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(id = ids,
value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))
positions <- data.frame(id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2))
datapoly <- merge(values, positions, by = c("id"))
# plot preperation (for many ggplots is especially decimals output of a function)
decimals <- 2
lim_min <- 3.1
lim_max <- 3.4
breaks <- seq(lim_min, lim_max, (lim_max - lim_min)/4)
labels <- c(paste0("<", sprintf(paste0("%.", decimals, "f"), breaks[1])),
sprintf(paste0("%.", decimals, "f"), breaks[2]),
sprintf(paste0("%.", decimals, "f"), breaks[3]),
sprintf(paste0("%.", decimals, "f"), breaks[4]),
paste0(">", sprintf(paste0("%.", decimals, "f"), breaks[5])))
# plot
ggplot(datapoly, aes(x = x, y = y)) +
geom_polygon(aes(fill = value, group = id)) +
scale_fill_gradient(limits = c(3.1, 3.4),
low = "green", high = "orange",
breaks = breaks,
labels = labels,
oob = squish)
Now the axis ticks labels are "<3.10" "3.17" "3.25" "3.33" ">3.40"
and I would like to have "<=3.10" "3.17" "3.25" "3.33" ">=3.40"
, but instead of >=
and <=
with the math symbols like here and here
Upvotes: 3
Views: 3282
Reputation: 404
So I found a solution by myself after the hint of baptiste to substitute
. Changing
labels <- c(substitute(""<=a, list(a =sprintf(paste0("%.", decimals, "f"), breaks[1]))),
sprintf(paste0("%.", decimals, "f"), breaks[2]),
sprintf(paste0("%.", decimals, "f"), breaks[3]),
sprintf(paste0("%.", decimals, "f"), breaks[4]),
substitute("">=a, list(a =sprintf(paste0("%.", decimals, "f"), breaks[5]))))
in the above latter example, results in what I wanted! Thank you!
Upvotes: 3
Reputation: 7153
Is this what you looking for?
out <- sprintf(paste0("%.", test, "f"), pi)
ggplot(data = iris, aes(x = Sepal.Length)) +
geom_bar() +
scale_x_continuous(name = paste(expression("<="), out))
Upvotes: 1