partial italic axis titles

10,959 views
Skip to first unread message

Felipe Carrillo

unread,
Jan 20, 2011, 2:40:51 PM1/20/11
to ggplot2

Hi:
I have put a long X axis title on my graph and want to partially make
some of the font italic.
the code below makes the whole title italic.
opts(axis.title.x = theme_text(colour = 'blue', angle = 45, size = 8,
face = 'italic'))

How can I make the words Fish fish words italic:
Estimated passage of Fish fish in 2001
Thanks in advance

Andrew Redd

unread,
Jan 20, 2011, 3:22:09 PM1/20/11
to Felipe Carrillo, ggplot2
opts(title = expresssion(paste("Estimated passage of ", italic("Fish fish", " in 2001")) )

see ?plotmath

-Andrew


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Jan 20, 2011, 3:16:47 PM1/20/11
to Felipe Carrillo, ggplot2
One way would be to use expression(). 


#create simple dataframe for plotting
xy <- data.frame(x=1:10, y=10:1)

ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
scale_x_continuous(expression(paste("Estimated passage of ",italic("Fish fish"),"in 2001")))
Although, there might be a more straightforward method...

B


Felipe Carrillo

unread,
Jan 20, 2011, 4:00:37 PM1/20/11
to Brandon Hurr, ggplot2
Thanks to both of you Brandon and Andrew. I am trying to put my italic words
inside the 'labs' function. I'll test it shortly.
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

Felipe Carrillo

unread,
Jan 20, 2011, 4:19:14 PM1/20/11
to ggplot2

 
It works but now my title isn't bold,,,I thought I've seen something like boldItalic option..

Felipe Carrillo

unread,
Jan 20, 2011, 5:27:56 PM1/20/11
to Andrew Redd, ggplot2
I figured out the bolditalic title problem, but now I am having problems with \n (newline). My X axis title
is long and would like to write it on two different lines. How can I use \n inside the function "expression"
Thanks

 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

From: Andrew Redd <amr...@gmail.com>
To: Felipe Carrillo <mazatla...@yahoo.com>
Cc: ggplot2 <ggp...@googlegroups.com>
Sent: Thu, January 20, 2011 12:22:09 PM
Subject: Re: partial italic axis titles

Dennis Murphy

unread,
Jan 21, 2011, 1:50:28 AM1/21/11
to Felipe Carrillo, ggplot2
Hi Felipe:


On Thu, Jan 20, 2011 at 2:27 PM, Felipe Carrillo <mazatla...@yahoo.com> wrote:
I figured out the bolditalic title problem, but now I am having problems with \n (newline). My X axis title
is long and would like to write it on two different lines. How can I use \n inside the function "expression"

You can't; it's documented on the plotmath help page that
"Control characters (e.g. \n) are not interpreted in character strings in plotmath, unlike normal plotting."

I did some searching of the R-help archives but came up empty on finding a solution that produces multiline expressions in plot labels. Hopefully someone will have better luck.

A sleazy little trick for non-expressions is something like

... + xlabs('Here is a
two-line subtitle.')

but expressions are too smart to be fooled by that ruse..I tried. Evidently, you need a cleverer trick :)

Maybe you can figure out how to do something along the lines of Marc Schwartz's suggestion in the following R-help post:
http://r.789695.n4.nabble.com/Plotmath-and-line-breaks-in-long-annotations-for-plots-td880718.html#a880719

The use of do.call(expression, ...) sounds like it might have some promise. The problem is that the solution is for text() in base graphics; you'd have to figure out how to translate to an equivalent in xlabs() or scale_*.

When someone figures this out, please post it; I'd be interested in the solution.

Hope this is of some help,
Dennis

baptiste auguie

unread,
Jan 21, 2011, 5:11:08 AM1/21/11
to Felipe Carrillo, ggplot2
Hi,

You could hack opts() with a custom grob for the x axis title,


library(ggplot2)

my.title = function(...) {
g1 <- textGrob(expression(bold("first line")),
0.5, 1, hjust = 0.5, vjust = 2)
g2 <- textGrob(expression(italic("second line")), 0.5, 0,
hjust = 0.5, vjust = 3)
gTree(children=gList(g1, g2))

}

qplot(1,1, geom="blank") + opts(axis.title.x = my.title,
plot.margin = unit(c(1,1,3,1), "lines"))

HTH,

baptiste


On Thu, Jan 20, 2011 at 11:27 PM, Felipe Carrillo

baptiste auguie

unread,
Jan 21, 2011, 10:09:13 AM1/21/11
to Felipe Carrillo, ggplot2
Actually, thinking about it it could be made cleaner with a little
help from gridExtra,


library(gridExtra)

my.title = function(expressions) {
function(...)
tableGrob(expressions, parse=TRUE,
theme=theme.list(gpar.corefill = gpar(fill = NA, col = NA),
core.just = "center"))
}

e = expression(alpha,"testing", italic(italic),
hat(beta), integral(f(x)*dx, a, b))

qplot(1,1, geom="blank") + opts(axis.title.x = my.title(e))


HTH,

baptiste

Felipe Carrillo

unread,
Jan 21, 2011, 10:42:28 AM1/21/11
to baptiste auguie, ggplot2
Thank you all for checking into this.
Baptiste, the first function works well, but I couldn't make the one
with gridExtra work. I get an error message:
Error in rep(c(list(gp.rowfille), list(gp.rowfillo)), nrow) :
  invalid 'times' argument
 
I might be missing a parenthesis somewhere..

baptiste auguie

unread,
Jan 21, 2011, 10:55:44 AM1/21/11
to Felipe Carrillo, ggplot2
Sorry, I'm not sure what happened but it seems a matrix() was missing
for it to work (I guess I was using a different version of gridExtra),

library(ggplot2)
library(gridExtra)

my.title = function(expressions) {
function(...)
tableGrob(matrix(expressions), parse=TRUE,


theme=theme.list(gpar.corefill = gpar(fill = NA, col = NA),
core.just = "center"))
}

e = expression(alpha,"testing", italic(italic),
hat(beta), integral(f(x)*dx, a, b))

qplot(1,1, geom="blank") + opts(axis.title.x = my.title(e))

baptiste


On Fri, Jan 21, 2011 at 4:42 PM, Felipe Carrillo

Zack Weinberg

unread,
Jan 21, 2011, 11:22:38 AM1/21/11
to Dennis Murphy, Felipe Carrillo, ggplot2
On Thu, Jan 20, 2011 at 10:50 PM, Dennis Murphy <djm...@gmail.com> wrote:
> Hi Felipe:
>
>
> On Thu, Jan 20, 2011 at 2:27 PM, Felipe Carrillo <mazatla...@yahoo.com>
> wrote:
>>
>> I figured out the bolditalic title problem, but now I am having problems
>> with \n (newline). My X axis title
>> is long and would like to write it on two different lines. How can I use
>> \n inside the function "expression"
>
> You can't; it's documented on the plotmath help page that
> "Control characters (e.g. \n) are not interpreted in character strings in
> plotmath, unlike normal plotting."
>
> I did some searching of the R-help archives but came up empty on finding a
> solution that produces multiline expressions in plot labels. Hopefully
> someone will have better luck.

What about atop("line one", "line two") ?

zw

Felipe Carrillo

unread,
Jan 21, 2011, 11:30:51 AM1/21/11
to baptiste auguie, ggplot2

 It works great now, I am trying to modify your function a little bit to
accomodate some italic font in
the middle of the first line but don't know much about grid. I want my first
line to say:
Figure 20. Actual passage of pacific species italic(Penaeus vannamei) migrating
to the San Francisco Bay newline
more text..........................

gitalic puts the italic font right above the first line and appears to ignore
the vjust and hjust arguments.
Would you mind giving me a hint on how to go about this?

my.test = function(...) {
  g1 <- textGrob(expression("Figure 20. Actual passage of pacific species"),0.5,

1, hjust = 0.5, vjust = 2)

  gitalic <- textGrob(expression(italic("Penaeus
vannamei"),2,vjust=0.5,hjust=-3))
 g2 <- textGrob(expression(italic("second line")), 0.5, 0,hjust = 0.5, vjust =
3)
 gTree(children=gList(g1, gitalic,g2))
 }
 qplot(1,1, geom="blank") + opts(axis.title.x = my.test,


             plot.margin = unit(c(1,1,3,1), "lines"))

Dennis Murphy

unread,
Jan 21, 2011, 7:14:42 PM1/21/11
to Felipe Carrillo, ggplot2
Hi Felipe:

How about this? I added a fontsize argument since the original seemed to come close to running off the page. Thanks to Baptiste for this approach; it makes a lot of sense.

my.test = function(...) {
  g1 <- textGrob(expression(paste("Figure 20. Actual passage of Pacific species ",
                  italic("Penaeus vannamei"), " migrating to the San Francisco Bay")),
                    0.5, 1, hjust = 0.5, vjust = 2, gp = gpar(fontsize = 10))
 g2 <- textGrob(expression(italic("second line")), 0.5, 0,hjust = 0.5, vjust = 3,
                           gp = gpar(fontsize = 10))
 gTree(children = gList(g1, g2))

 }
 qplot(1,1, geom="blank") + opts(axis.title.x = my.test,
             plot.margin = unit(c(1,1,3,1), "lines"))

HTH,
Dennis

baptiste auguie

unread,
Jan 22, 2011, 6:04:31 AM1/22/11
to Dennis Murphy, Felipe Carrillo, ggplot2
The tableGrob approach has the advantage of being more versatile
(arbitrary number of rows, automatic line spacing), and doesn't
require to manually adjust the ggplot plot margins,

library(gridExtra)

my.title = function(expressions) {
function(...)
tableGrob(matrix(expressions), parse=TRUE,
theme=theme.list(gpar.corefill = gpar(fill = NA, col = NA),
core.just = "center"))
}

title <- expression(paste("Figure 20. Actual passage of Pacific


species ", italic("Penaeus vannamei"),

" migrating to the San Francisco Bay"), italic("second line"))

qplot(1,1, geom="blank") + opts(axis.title.x = my.title(title))

That said, I would personally place the figure title outside of the
plot, not in the axis label,


library(gridExtra)


p <- qplot(1,1, geom="blank") + xlab("")

caption <- tableGrob(matrix(expression(paste("Figure 20. Actual


passage of Pacific species ", italic("Penaeus vannamei"),

" migrating to the San Francisco Bay"), italic("second line"))),


parse=TRUE, theme=theme.list(gpar.corefill =
gpar(fill = NA, col = NA),
core.just = "center"))


grid.arrange(p, sub=caption)

HTH,

baptiste

Felipe Carrillo

unread,
Jan 22, 2011, 11:14:47 AM1/22/11
to Dennis Murphy, ggplot2
Deniis:
Great! First time I try to use expressions. Thanks for taking the time to expand this nice function that Baptiste started.
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

Felipe Carrillo

unread,
Jan 22, 2011, 10:52:14 AM1/22/11
to baptiste auguie, Dennis Murphy, ggplot2
Thank you much Baptiste, Is exactly what I needed. I just couldn't find the way
to put
the caption outside the graph. I wish this could be added to ggplot2 to have
more control
over captions. :-(

 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx
Reply all
Reply to author
Forward
0 new messages