3

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

2
  • what's the expression you're trying to display? And why is sprintf essential? (as opposed to bquote or substitute, for instance)? Commented Apr 12, 2017 at 7:36
  • The expression I want to display is the above one. I thought sprintf is essential for me, because I use it originally in the labels for the axis ticks and these should have all the same number of decimals, e.g. c("0.00", "0.01",...). The number of decimals is different for many ggplots and test is the optional variable to identify the required number of decimals. I used this as a way to "generalize" my plots, because it's not possible to have a look at each one. Maybe I should add a better example? Commented Apr 12, 2017 at 7:41

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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

enter image description here

3 Comments

But then I loose the math symbol for "smaller or equal than"?
what do you mean by losing the math? scale_x_continuous(name) argument only provide the axis label. Please edit the question to show what is it that you wanted to achieve.
I meant, I would like to have math symbols like this and this one instead of <= and >=. I edited the question and add a better example. Sorry for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.