Changing specific character in axis text

40 views
Skip to first unread message

thomas

unread,
Nov 21, 2010, 1:52:29 PM11/21/10
to ggplot2
Hi,

is it possible to change the first character of each y-axis text label
to a different color and/or making it bold?
If so... how? ;)

I.e.:
http://had.co.nz/ggplot2/graphics/c2a2d54cde70b66c57f704ec9a06483d.png
I would like the character '1' from the first label ("1000") to be
bold, character '2' from the second label ("2000") etc.

Thank you,
Thomas

Zack Weinberg

unread,
Nov 21, 2010, 2:36:15 PM11/21/10
to thomas, ggplot2

If this is possible, I bet it is done by manually providing labels,
and then somehow using ?plotmath expressions rather than character
strings for those labels. The documentation, unfortunately, makes it
sound like only character strings work. I would love to be wrong
about that.

(Have I kvetched about the documentation recently? The reference page
for *every function* should document *every one of that function's
arguments* in enough detail that you could use them correctly even if
you had read *none* of the other documentation. Nearly all R built-in
documentation gets this right, but ggplot (and reshape) don't.)

zw

baptiste auguie

unread,
Nov 21, 2010, 2:46:21 PM11/21/10
to thomas, ggplot2
Hi,

It's probably easiest with the tikzDevice device, where you could
encode LaTeX syntax in your axis labels. Something like,

library(ggplot2)
library(tikzDevice)

p = qplot(1:9, 1:9 * 1e3) +
scale_y_continuous(breaks= seq(1,9)*1e3, labels =
paste("\\textbf{",seq(1,9),"}000", sep=""))

tikz("test.tex", standAlone=TRUE)
print(p)
dev.off()

HTH,

baptiste

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

Joshua Wiley

unread,
Nov 21, 2010, 3:41:52 PM11/21/10
to Zack Weinberg, thomas, ggplot2
On Sun, Nov 21, 2010 at 11:36 AM, Zack Weinberg <za...@panix.com> wrote:
> On Sun, Nov 21, 2010 at 10:52 AM, thomas <thk....@gmail.com> wrote:
>> Hi,
>>
>> is it possible to change the first character of each y-axis text label
>> to a different color and/or making it bold?
>> If so... how? ;)

Baptiste's solution seems pretty elegant to me. It is also possible
to do using expressions and some of the formatters from plotmath().
But possible does not equate to easy.

> If this is possible, I bet it is done by manually providing labels,
> and then somehow using ?plotmath expressions rather than character
> strings for those labels.  The documentation, unfortunately, makes it
> sound like only character strings work.  I would love to be wrong
> about that.

Well it's your lucky day then Zack ;) Here's an example:

qplot(1:10, 1:10, geom = "point") +
scale_x_continuous(breaks = c(1, 5, 10),
labels = expression(bold("1.")~0, bold("5.")~0, bold("10.")~0))

I am not very good at plotmath expressions...I've never really had to
do more than add the occasional Greek letter, so I'm not certain how
you can indicate separate elements without adding a space or anything
(hence my ~).

> (Have I kvetched about the documentation recently?  The reference page
> for *every function* should document *every one of that function's

I suspect if you were to write some nice documentation and email it to
Hadley, he would gladly add it. I have (and still am) actually
considered this myself, but it is not as easy as it might seem.
Firstly, there is the non-trivial time required to do it. Secondly,
ggplot2 is somewhat unique, I think, in that virtually none of the
functions operate in isolation. This makes documenting them in
isolation difficult. To make an analogy, how would you describe what
a noun is and how to use a noun properly? A verb? A direct object?
Just like "ran" is a perfectly fine verb yet, "I will ran to the
store" is an incorrect sentence, so to certain functions are properly
or improperly used only within the context of the broader grammar of
graphics. This is certainly documentable, and it would be wonderful
if it was, but it is more difficult, I think, then just writing
definitions out argument by argument. I am personally of the opinion
that I would prefer Hadley's time, expertise, and overall genius to be
devoted towards development, which leaves documentation to others/the
community. I suppose one thing to consider is, would you like more
documentation even if it was incomplete/not perfect? It may be
helpful to have something even if it does not fully grasp the nuances
and how usage changes depending on the context. If so, with Hadley's
blessing, I would be willing to start on this, perhaps originally
through the wiki (so others can edit my errors), and then eventually
translate it into the documentation files?

> arguments* in enough detail that you could use them correctly even if
> you had read *none* of the other documentation.  Nearly all R built-in
> documentation gets this right, but ggplot (and reshape) don't.)
>
> zw
>

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

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

thomas

unread,
Nov 21, 2010, 4:00:27 PM11/21/10
to ggplot2
Thanks for answering guys.

> Baptiste's solution seems pretty elegant to me.  It is also possible
> to do using expressions and some of the formatters from plotmath().
> But possible does not equate to easy.

I find tikzdevice never to be easy either though --- i.e. proper
scaling.
So I will try expression() and so forth.

as for concating without a space:
expression(paste(bold("1"),"000"))

> If so, with Hadley's blessing, I would be willing to start on this, perhaps originally
> through the wiki (so others can edit my errors), and then eventually
> translate it into the documentation files?

If I may chime in here. I find documentations without user comments to
be pretty annoying nowadays.
One should be allowed to comment on the ggplot2 documentation right
away.

Thomas

Hadley Wickham

unread,
Dec 13, 2010, 1:32:24 PM12/13/10
to Joshua Wiley, Zack Weinberg, thomas, ggplot2
> I suspect if you were to write some nice documentation and email it to
> Hadley, he would gladly add it.  I have (and still am) actually
> considered this myself, but it is not as easy as it might seem.
> Firstly, there is the non-trivial time required to do it.  Secondly,
> ggplot2 is somewhat unique, I think, in that virtually none of the
> functions operate in isolation.  This makes documenting them in
> isolation difficult.

Definitely and great analogy!

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages