Hi:
Another long-winded post to justify the time I put into it :) After a
fair bit of trial and error, the following pair of examples work.
classes <- cut(seq(0.7, 1.5, by=0.1), breaks=c(-Inf, 0.8, 1, 1.25, Inf),
right=FALSE)
# The first part of the game is to write the plotmath code for the
# tick labels as a vector of character strings.
# Since > and >= are binary operators in plotmath, we need to
# 'fill in' the left hand side with something, so we put in a phantom
# empty string in the first and last labels; we also use single
# quotes to enclose the code because we're using double quotes
# inside of phantom().
txtlabs <- c('phantom("") > 0.8', '0.8 - 1', '1 - 1.25',
'phantom("") >= 1.25')
# Parse the strings into a length four expression object
exprlev <- parse(text=txtlabs)
# This is the 'easy' way to do it
DF <- data.frame(class=classes)
require("ggplot2")
ggplot(data=DF, aes(x=class)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_x_discrete(labels = exprlev) +
labs(y = "Relative frequency")
# Ramnath's method from the StackOverflow post linked by Brandon also works,
# but this is the hard way:
ggplot(data = DF, aes(x = class)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
labs(y = "Relative frequency") +
scale_x_discrete(labels = c("[-Inf,0.8)" = expression(phantom("") > 0.8),
"[0.8,1)" = "0.8 - 1",
"[1,1.25)" = "1 - 1.25",
"[1.25, Inf)" = expression(phantom("")
>= 1.25)))
#####
I thought this should be something the scales package could handle
easily, but it didn't work on this example code chunk:
require(scales)
ggplot(data = DF, aes(x = class)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
labs(y = "Relative frequency") +
scale_x_discrete(labels = parse_format()(txtlabs))
The reason is that the code "parse_format()(txtlabs)" returns a list
of expression objects, whereas exprlev is an expression object of
length 4. Since the latter worked, and I know from past experience
that one can pass expression objects to axis tick labels, we might
make some progress by running do.call() on the list of expressions
returned by parse_format().
> parse_format()(txtlabs)
[[1]]
expression(phantom("") > 0.8)
[[2]]
expression(0.8 - 1)
[[3]]
expression(1 - 1.25)
[[4]]
expression(phantom("") >= 1.25)
> exprlev
expression(phantom("") > 0.8, 0.8 - 1, 1 - 1.25, phantom("") >= 1.25)
> do.call(c, parse_format()(txtlabs))
expression(phantom("") > 0.8, 0.8 - 1, 1 - 1.25, phantom("") >=
1.25)
The last two objects are not identical because exprlev contains a
number of attributes that are stripped from the result of do.call().
However, they are close enough to give the latter a shot, and as it
turns out,
plugging in the last call to the labels = argument of
scale_x_discrete() works, too:
#####
# The medium hard way, but useful to know :)
ggplot(data = DF, aes(x = class)) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
labs(y = "Relative frequency") +
scale_x_discrete(labels = do.call(c, parse_format()(txtlabs)))
So now we have three solutions :)
######
And just for the fun of it, we can also use the scales package
functions in base graphics (which is not widely known, so here's an
unsolicited advertisement):
barplot(table(classes), xaxt = "n")
axis(1, at = seq_along(levels(classes)),
labels = do.call(c, parse_format()(txtlabs)))
barplot(table(classes), xaxt = "n")
axis(1, at = seq_along(levels(classes)),
labels = exprlev)
#########
Dennis
On Tue, Oct 29, 2013 at 8:24 AM, Brandon Hurr <
brando...@gmail.com> wrote:
> ≥ ≤