Different font faces within a single legend

54 views
Skip to first unread message

Karolina Maciag

unread,
Jul 8, 2015, 4:08:32 PM7/8/15
to ggp...@googlegroups.com
Is it possible to italicize only certain legend entries in a ggplot?

I have a geom_line plot in which one of the legends represents genotype. By convention in our field, one of these (the control) should be in plain font, while others (experimental knockout genotype(s)) should be italicized.

I have factorized the genotypes and tried
legend.text = element_text(face = c("plain","italic"))
but this doesn't make a difference.

Here is an example, with some quick dummy data
timecourse <- data.frame(rbind(abs(rnorm(10)),abs(rnorm(10))))
names(timecourse) <- prettyNum(10*abs(rnorm(10)),digits=3)
timecourse$Genotype <- as.factor(c("ControlGenotype","GeneX-/-"))
timecourse <- reshape2:::melt.data.frame(timecourse, id.vars="Genotype",variable.name="Timept", value.name="Y")
p <- ggplot(data=timecourse, aes(x=Timept, y=Y, color=Genotype, group=Genotype))
p <- p + geom_line(aes(), size=0.7) + geom_point(aes(), size=2.5) + scale_color_manual(values=c("#000000", "blue"))
p <- p + labs(x="Time (hrs)", y="Readout of some bioassay")
p <- p + theme (legend.text = element_text(face = c("plain","italic")))

"ControlGenotype" should be plain
"GeneX-/-" should be italicized (but is not)

Thanks very much,
Karolina


--

Karolina Maciag
MD/PhD Candidate, Harvard Medical School/MIT-HST
Hacohen Lab, Broad Institute

Dennis Murphy

unread,
Jul 8, 2015, 4:37:19 PM7/8/15
to Karolina Maciag, ggplot2
Hi:

Try this:

labl <- list("ControlGenotype", expression(italic("GeneX-/-")))

library(ggplot2)
ggplot(data=timecourse, aes(x=Timept, y=Y, color=Genotype, group=Genotype)) +
geom_line(size=0.7) + geom_point(size=2.5) +
scale_color_manual(values=c("#000000", "blue"),
labels = labl) +
labs(x="Time (hrs)", y="Readout of some bioassay")


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

Brian

unread,
Jul 8, 2015, 5:12:42 PM7/8/15
to Dennis Murphy, Karolina Maciag, ggplot2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Beat me to it.

Why does changing
labl <- list("ControlGenotype", expression(italic("GeneX-/-")))
to
labl <- c("ControlGenotype", expression(italic("GeneX-/-")))
alter the justification?

So:

labl <- c("ControlGenotype", expression(italic("GeneX-/-")))
ggplot(data=timecourse, aes(x=Timept, y=Y, color=Genotype,
group=Genotype)) +
geom_line(size=0.7) + geom_point(size=2.5) +
scale_color_manual(values=c("#000000", "blue"),
labels = labl) +
labs(x="Time (hrs)", y="Readout of some bioassay")


Thanks
Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJVnZIuAAoJEB+kbV9VX51IB0oQAINzlC/L/+gstCkdl1rnhHtH
AUP2UEd9wwlRfBCm+JANZjolg9CavKgLA2QnG1pFDM3RRGeUvIm2FHtZK0Tnr2hb
CHAXM8Hj3+cy5onbggoY3vYweRa/UKKWgw9F/95tpo/HFbcPdJmU3UFD8/Cq0oHw
h06laTYW2WL4z0Winv0mIh6bUE5WLQ8f0VDoaRhjsLSPelk1A3mZotepbXkuygPA
XLNx88ePpp6jRsLzzPaisZ8sYgX+84YJNKomYGW2j1fHbeRN0sObe3vqoXV9n2dL
KEpBWGbAf0ChL36eL+VYvqCNZzRXI9LTXx8jMCqSeluRQxyTwdWScd72u3ijCwG7
yRZbmdQCSOimSl9QGsR52C4Ryf5ShFeb8YInokvU5vm6qmH6eS3SrwXtVdys2Lpb
CG8DaY6aCdizghJARNDHR/huV1EntyrvdLQbtTS7R3B/vRk763peS7hFk7EP31T/
jvxoSkg4/rp/LlOaeMPxHWP0c9DUzOcX9zAKG2P1ynoRFSfB/l9/Dr2t9caN/59R
Pl0OML8JOwCXE0j4/qWFMKEJMRpHx1bkO1Xyff2wj2a6isO18qUoG9nuT4ZxLIuS
6crqFZhTo9Nz5yJzv4KaycMzznvtimTdT9Znqt+U9ygvZ00H/jrXUWEbGarXvSZL
09pkGisIDLr5hdeVu+sW
=F2zT
-----END PGP SIGNATURE-----

Karolina Maciag

unread,
Jul 17, 2015, 1:22:43 PM7/17/15
to Brian, Dennis Murphy, ggplot2
Thank you both - quite belated but I am really grateful.

It was a huge help - affects nearly every figure in my paper (and likely future papers as well), unless journal standards force the use of image editing software for legends.

I finally figured out on my own how to include Greek characters and newlines both in facet labels... everyone else I'd talked to about that had given up and used Illustrator!

More and more people in our lab are moving over to R for data exploration and figure generation, but we will need to make a handy cheat-sheet for the formatting in order to take the sweat out of annotating graphs.

Thanks again,
Karolina
Reply all
Reply to author
Forward
0 new messages