"greater or equal" symbol in an axis tick label

882 views
Skip to first unread message

Stéphane Laurent

unread,
Oct 29, 2013, 9:54:25 AM10/29/13
to ggp...@googlegroups.com
Hello,

> classes <-  cut(seq(0.7,1.5,by=0.1), breaks=c(-Inf,0.8,1,1.25,Inf), right=FALSE)
> levels(classes)
[1] "[-Inf,0.8)"  "[0.8,1)"     "[1,1.25)"    "[1.25, Inf)"
 
I'd like to use "<0.8" instead of "[-Inf, 0.8)" and ">=1.25" instead of "[1.25,Inf)". I have not found any way to get a "greater or equal" symbol which works in the axis tick labels :

 levels(classes) <- c("<0.8", "[0.8,1)", "[1,1.25)", " ??? 1.25")
 df <- data.frame(class=classes)
 ggplot(data=df, aes(x=class)) + geom_bar(aes(y = (..count..)/sum(..count..)))

Brandon Hurr

unread,
Oct 29, 2013, 11:24:29 AM10/29/13
to Stéphane Laurent, ggplot2
≥ ≤

Does it work if you copy and paste them? 

If not, try using expressions. See here:



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dennis Murphy

unread,
Oct 29, 2013, 6:51:58 PM10/29/13
to Brandon Hurr, Stéphane Laurent, ggplot2
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:
> ≥ ≤

Stéphane Laurent

unread,
Oct 30, 2013, 5:27:55 AM10/30/13
to ggp...@googlegroups.com, Brandon Hurr, Stéphane Laurent
Thank you ! I use the first solution and it works well. 

I have written a code such that the user only has to specifiy the vector of break points :

breaks <- c(0.8, 1, 1.25)
classes <-  cut(seq(0.9, 1.5, by=0.1), breaks=c(-Inf, breaks, Inf),
                right=FALSE)
txtlabs <- c(paste0('phantom("") < ', breaks[1]), 
             sapply(1:(length(breaks)-1), function(i) paste0('group("[",list(', breaks[i], ",", breaks[i+1], '),")")')),
             paste0('phantom("") >= ', tail(breaks,1))
)
exprlev <- parse(text=txtlabs)

require("ggplot2")
DF <- data.frame(class=classes)
ggplot(data=DF, aes(x=class)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) +
  scale_x_discrete(labels = exprlev, drop=FALSE) +
  labs(y = "Relative frequency") + 
  theme(axis.text.x=element_text(size = 11, colour="black", angle=30, vjust=0.5)) 

 I hope this warning is not dangerous:

Warning message:
In is.na(labels) :
  is.na() applied to non-(list or vector) of type 'expression'

Reply all
Reply to author
Forward
0 new messages