Latex type labels?

278 views
Skip to first unread message

Ignacio Martinez

unread,
Nov 11, 2012, 5:35:19 PM11/11/12
to ggp...@googlegroups.com
I'm trying to make some modification to a graph i created using ggplot2.
First, I would like to change y$variable for a different text
Second, I would like to change FT, PT, NW for mathematical expressions.
Can somebody explain me how to do this?

Thanks!

-Ignacio

PS: This is the code i'm using:

ggplot(y, aes(x=y$"epsilon_h", y=y$value, color=y$variable)) + xlim(-5000, -100) + 
  xlab(expression(epsilon[h])) + ylab("value") +  geom_point(shape=1) +
  scale_colour_hue(l=50) + # Use a slightly darker palette than normal
  geom_smooth(method=lm,   # Add linear regression lines
              se=FALSE,    # Don't add shaded confidence region
              fullrange=T)  # Extend regression lines

Ignacio Martinez

unread,
Nov 11, 2012, 5:57:16 PM11/11/12
to ggp...@googlegroups.com
This is the code that generates the graph that i have right now:

y<-structure(list(epsilon_h = c(-4800, -2800, -4800, -2800, -4800, 
-2800), variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("FT", 
"PT", "NW"), class = "factor"), value = c(25032.8682913, 15032.8682913, 
7227.4538261, 5227.4538261, 7270.04754131, 7270.04754131)), .Names = c("epsilon_h", 
"variable", "value"), row.names = c(NA, -6L), class = "data.frame")
-2L))
ggplot(y, aes(x=y$"epsilon_h", y=y$value, color=y$variable)) + xlim(-5000, -100) + 
xlab(expression(epsilon[h])) + ylab("value") +  geom_point(shape=1) +
scale_colour_hue(l=50) + # Use a slightly darker palette than normal
geom_smooth(method=lm,   # Add linear regression lines
se=FALSE,    # Don't add shaded confidence region
fullrange=T, size = 1.5, aes(linetype=y$variable))   # Extend regression lines

First, I would like to change y$variable for a different text
Second, I would like to change FT, PT, NW for mathematical expressions.

Thanks!,
-Ignacio

PS: Sorry for the first email, i just learned how to use dput()


Dennis Murphy

unread,
Nov 11, 2012, 6:42:21 PM11/11/12
to Ignacio Martinez, ggp...@googlegroups.com
Hi:

This isn't a perfect representation of your graph and its requirements, but here's how you define expressions as labels in a legend guide and pass them into a ggplot() call.

# Create a list of expressions of the same length as 
# the number of unique levels in variable
varexp <- list(expression(alpha + beta*x), expression(gamma + delta*x),
                expression(zeta + theta*x))

# Since y is passed in as the input data frame, you don't need
# to say y$value, y$variable, y$"epsilon_h" in ggplot()
ggplot(y, aes(x = epsilon_h, y = value, color = variable)) +
    geom_point(shape = 1) +
    scale_color_manual(breaks = levels(y$variable), 
                       values = c("blue", "red", "green"),
                       labels = varexp) +
    geom_smooth(method = lm, aes(linetype = variable), se = FALSE, fullrange = TRUE, size = 1.5) +
    labs(x = expression(epsilon[h]), y = "value") +
    xlim(-5000, -100) +
    scale_linetype_manual(breaks = levels(y$variable), 
                          values = c("solid", "dashed", "dotdash"),
                          labels = varexp) +
    theme(legend.text = element_text(size = 12))

I tried using scale_color_hue() with leading arguments breaks, values and labels, but it returned an error:
Error in discrete_scale("colour", "hue", hue_pal(h, c, l, h.start, direction),  : 
  unused argument(s) (values = c("blue", "red", "green"))

According to the help page, scale_*_hue() is supposed to be able to take such arguments at the beginning of the call and pass them to discrete_scale(), but I couldn't get that to work. Instead, I used scale_color_manual() and scale_linetype_manual()  above. To darken the color hues, it appears that you'll need to find the hex values of the colors you want and substitute them for c("red", "blue", "green") in the values argument of scale_color_manual(). ggplot2 will not let you construct multiple legends for the same aesthetic.

On the off chance that you were not aware of this fact, if you want to merge two scales into one, they need to have the same breaks and labels, which is why varexp is passed as the value of the labels = argument in both legends.

Dennis

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

Ignacio Martinez

unread,
Nov 11, 2012, 7:39:14 PM11/11/12
to Dennis Murphy, ggp...@googlegroups.com
Thanks a lot Dennis!

Just 2 more questions:
1. How can I have a formula instead of "variable" as the title of the legends?
2. How can I write f(cdot) where cdot is a centered dot?

Thanks again

Dennis Murphy

unread,
Nov 11, 2012, 8:16:23 PM11/11/12
to Ignacio Martinez, ggp...@googlegroups.com
Hi:

On Sun, Nov 11, 2012 at 4:39 PM, Ignacio Martinez <igna...@gmail.com> wrote:
Thanks a lot Dennis!

Just 2 more questions:
1. How can I have a formula instead of "variable" as the title of the legends?

Use the labs() function and write an expression as the value of both color and linetype; you need to make the same title for both if the legends are to be merged (i.e., you really need the legend title, breaks and labels to be the same in a merged legend...which of course occurred to me about five seconds after sending my previous reply...)
 
2. How can I write f(cdot) where cdot is a centered dot?

R uses plotmath() for mathematical symbols in expressions, and AFAICT there is no function to render a single centered dot in an expression, although there is a cdots function that generates a raised ellipsis for sums, products and the like. See ?plotmath for the symbols and operators it supports. 

D.

Ignacio Martinez

unread,
Nov 12, 2012, 8:43:13 AM11/12/12
to Dennis Murphy, ggp...@googlegroups.com
You are awesome! 
Thanks!
Reply all
Reply to author
Forward
0 new messages