Re: Mathematical symbols in legends

444 views
Skip to first unread message

Roman Luštrik

unread,
Jan 2, 2013, 5:50:50 PM1/2/13
to Brian, ggp...@googlegroups.com
Can you provide a small reproducible example? Looks like your pdf device is acting up. What OS are you on?

Cheers,
Roman



On Wed, Jan 2, 2013 at 6:41 PM, Brian <brian...@mac.com> wrote:
I've been trying to insert a mathematical symbol as a legend label.  Specifically, I want to have groups such as "Age <60" and "Age ≥60".  These print OK when I plot the graph on the screen but the symbol doesn't come out when I write the graph to a pdf. I can use expression(Age >= 60) for the labels on the axes, but get an error when I try to use it for the legend labels.

Warning message:

In is.na(labels) :

  is.na() applied to non-(list or vector) of type 'expression'


Any ideas?

Thanks very much,
Brian

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



--
In God we trust, all others bring data.

Dennis Murphy

unread,
Jan 2, 2013, 6:52:25 PM1/2/13
to Brian, ggp...@googlegroups.com
Hi:

Here is a toy example:

DF <- data.frame(age = rpois(40, 55), y = rnorm(40, 10))
DF$Age <- cut(DF$age, c(0, 60, 200), labels = c("< 60", "60+"),
include.lowest = TRUE)

# Set up legend labels with mathematical symbols
labs <- list(expression(Age < 60), expression(Age >= 60))

library(ggplot2)
ggplot(DF, aes(x = age, y = y, color = Age)) +
geom_point() +
scale_color_manual(breaks = levels(DF$Age),
values = c("red", "blue"),
labels = labs)

Dennis

Brian Shine

unread,
Jan 2, 2013, 7:11:02 PM1/2/13
to Roman Luštrik, Brian Shine, ggp...@googlegroups.com
Here are the data:

aghb <- data.frame(GHB = rep(1:4,3), Age = rep(1:3, each = 4), R = c(91.9, 79.1, 91.5, 104.6, 126.5, 144.6, 156.9, 187.8, 225.8, 257.3, 230.6, 241.2) )

and here's the code to produce bar chart.

labs <- list("<6.5%", "<7.5%", "<8.5%", expression("" >= 8.5))
labs2 <- list("<45", "<65", expression("" >= 65))
exp2 <- expression("" >= 8.5)
p1 <- ggplot(aghb, aes(x = factor(GHB), y = R, fill = factor(Age, labels = labs2))) + geom_bar(position = "dodge", stat = "identity") + labs(title = "Mortality rate by HbA1c and age in patients on renal replacement therapy", x = "HbA1c (%)", y = "Mortality / 1000 patient years", fill = "Age") 
p1 + scale_x_discrete(labels=labs)

That gives me the following:

PastedGraphic-1.pdf
PastedGraphic-2.pdf
Reply all
Reply to author
Forward
0 new messages