Using expression in legend labels: d is at least x and less than y

502 views
Skip to first unread message

Crump, Ron

unread,
Aug 21, 2015, 6:02:34 AM8/21/15
to ggp...@googlegroups.com
Hi,

Having a little problem getting expressions into my legend labels.

Here is some example data:
zz <- structure(list(x = c(1970L, 1971L, 1972L, 1973L, 1974L, 1975L,
1976L, 1977L, 1978L, 1979L, 1980L, 1970L, 1971L, 1972L, 1973L,
1974L, 1975L, 1976L, 1977L, 1978L, 1979L, 1980L, 1970L, 1971L,
1972L, 1973L, 1974L, 1975L, 1976L, 1977L, 1978L, 1979L, 1980L
), y = c(0.970143565209582, 0.425199685618281, 0.748703769175336,
0.749315701657906, 0.468444062629715, 0.994439253117889, 0.161479195114225,
0.536185430362821, 0.747455008327961, 0.0604149552527815,
0.183885730803013,
0.31202163011767, 0.707220257958397, 0.569816916249692, 0.640419519972056,
0.93991412059404, 0.116871100850403, 0.893233464099467, 0.831902656704187,
0.172733208630234, 0.265650319634005, 0.063512880820781, 0.426135629182681,
0.523865050170571, 0.632733916630968, 0.216925758402795, 0.20172484847717,
0.0342107121832669, 0.912192743737251, 0.988305125385523,
0.598734048428014,
0.0579233700409532, 0.482535298913717), z = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label =
c("a",
"b", "c"), class = "factor")), .Names = c("x", "y", "z"), row.names = c(NA,
-33L), class = "data.frame")


I¹d like to have "Measurement year, d² as the legend title, with a line
break between Measurement and year. This works in the example code below,
except that the gap between the comma and d is too big.

The legend labels should then indicate that: d < 1972, 1972 <= d < 1977
and d >= 1977. The second of these expressions, in which there is a <= and
a < is giving me difficulty. If just make it 1972 <= d it works, but
obviously isn¹t what I want to say.

The code is:
ggplot(zz,aes(x=x,y=y,colour=z))+geom_line()+
scale_colour_manual(values=c("red","blue","green"),name=expression(paste("M
easurement\nyear,",italic(d),sep="")),labels=expression(italic(d)<1972,1972
<= italic(d) < 1977, italic(d) >= 1977))

And gives the error:
Error: unexpected '<' in
"ggplot(zz,aes(x=x,y=y,colour=z))+geom_line()+scale_colour_manual(values=c(
"red","blue","green"),name=expression(paste("Measurement\nyear,",italic(d),
sep="")),labels=expression(italic(d)<1972,1²

If I put ³(1972 <= italic(d)),1977², the code runs but I get the extra
brackets.

So I¹ve got 2 little problems there: (a) spacing in the legend title and
(b) having a greater than or equals and a less than in the same expression.

My actual plot uses far more data and requires a number of these ³is
between² bits in the legend labels.

Thanks for your time,
Ron.

Brian

unread,
Aug 21, 2015, 7:20:47 AM8/21/15
to Crump, Ron, ggp...@googlegroups.com
Hi Ron,

yeah the expression syntax is probably one of the strangest of things of
R... I think the python people did a better by just mimicking/copying
latex syntax... Disclaimer: I still don't fully understand the power of
expression, so that is a bit of *user* bickering.


Rigorous mathematical notation is enforced here by R (and I can't figure
out how to do it otherwise...)

ggplot(zz,aes(x=x,y=y,colour=z))+geom_line()+

scale_colour_manual(values=c("red","blue","green"),name=expression(atop(Measurement~year,
italic(d))),
labels=expression(group("(", list(-infinity,
1972), ")"), group("[", list(1972, 1977), ")"), group("[", list(1977,
infinity), ")")))

I hope that works for you. Note: If you have a beginning of your data
set and are not going backward or forward to infinity, the labels
returned by "cut" will suffice, and there's no need for all this manual
labor.

For example:


zz$interval <- cut(zz$x, c(1900, 1972, 1977, 2015), dig.lab = 4)
ggplot(zz,aes(x=x,y=y,colour=interval))+geom_line()+

scale_colour_manual(values=c("red","blue","green"),name=expression(atop(Measurement~year,
italic(d))))


Cheers,
Brian

Crump, Ron

unread,
Aug 21, 2015, 8:04:44 AM8/21/15
to ggp...@googlegroups.com
Thanks to Brian and David for their suggestions.

I¹m going to go with Brian¹s, at least for now, as it gets me going again
(even though I prefer the notation I was trying to achieve aesthetically).

David,

>Following code seems can solve the problems that you had.
>
>ggplot(zz,aes(x=x,y=y,colour=z))+geom_line()+
>scale_colour_manual(values=c("red","blue","green"),name=expression(paste("
>Measurement\n", paste("year, ",italic(d)))),
>labels=expression(paste(italic(d)<1972), paste("1972 <=", italic(d), "<
>1977"), paste(italic(d) >= 1977)))

Except that the ³<=³ within the paste is not parsed into the \ge symbol.

Thanks again,

Ron.

Hadley Wickham

unread,
Aug 21, 2015, 8:25:05 AM8/21/15
to Crump, Ron, ggp...@googlegroups.com
The problem is that x < y <= z is not a valid R expression, so you
need to convert it to something that is. (x < y) <= z is valid, but
will add visible parens. If you carefully read ?plotmath, you'll
notice that you can use {} for "invisible" grouping, so the code you
want is:

ggplot(zz, aes(x, y, colour = z)) +
geom_line() +
scale_colour_manual(
values = c("red", "blue", "green"),
name = expression("Measurement\nyear," * italic(d)),
labels = expression(
italic(d) < 1972,
{1972 <= italic(d)} < 1977,
italic(d) >= 1977
)
)

Also note the use of * instead of paste().

Hadley
> --
> --
> 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/d/optout.



--
http://had.co.nz/

Crump, Ron

unread,
Aug 21, 2015, 8:58:23 AM8/21/15
to Hadley Wickham, Crump, Ron, ggp...@googlegroups.com
Hadley,

That is just what I needed. Thanks for that.

Ron.
Reply all
Reply to author
Forward
0 new messages