bar plot example gives me an error

61 views
Skip to first unread message

DenisChabot

unread,
Apr 16, 2011, 6:13:20 PM4/16/11
to ggplot2
Hi,

With "base" graphics, I have produced plots showing the abundance of
different prey items in fish stomachs.

An example is available here: <http://dl.dropbox.com/u/612902/Morues
%20proies%20tfil.pdf>

I don't have to tell you that doing "facetting" with a for loop and
base graphics is more laborious than what ggplot2 offers.

I looked for help on internet to produce this type of figure with
ggplot2 and found this:

<http://learnr.wordpress.com/2009/03/17/ggplot2-barplots/>

The 5th figure is quite pretty much what I want to accomplish.
However, I get an error when trying to reproduce this example,
possibly because I use a different version of R or ggplot2?

R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] fr_CA.UTF-8/en_US.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] grid stats graphics grDevices utils datasets
methods base

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.1 reshape_0.8.4 plyr_1.5.1

loaded via a namespace (and not attached):
[1] digest_0.4.2 tools_2.13.0


So if I try to repeat the example from learnr:


a <- ggplot(df.m, aes(x = Period, y = value/1e+06,
fill = Region)) + opts(title = "Migration to the United States
by Source Region (1820-2006)") +
labs(x = NULL, y = "Number of People (in millions)",
fill = NULL)
b <- a + geom_bar(stat = "identity", position = "stack")
b
Erreur dans titles[[i]] : indice hors limites

So I cannot produce the second plot for the example. Nor the 3rd one:

b <- b + scale_fill_brewer(palette = "Set1")
b
Erreur dans titles[[i]] : indice hors limites

Then the author changes some aspects of the theme and suddenly I
succeed:

immigration_theme <- theme_update(axis.text.x = theme_text(angle = 90,
hjust = 1), panel.grid.major = theme_line(colour = "grey90"),
panel.grid.minor = theme_blank(), panel.background =
theme_blank(),
axis.ticks = theme_blank(), legend.position = "none")
b

c <- b + facet_grid(Region ~ .) + opts(legend.position = "none")
c

What is wrong with the steps that should have resulted in the plots 2
and 3 from the learnr page?

Thanks in advance,

Denis

Kohske Takahashi

unread,
Apr 17, 2011, 5:31:59 AM4/17/11
to DenisChabot, ggplot2
Hi,

labs(fill=NULL) indeces the error.
try labs(fill="")

the last example surpress the legend display for fill by
opts(lenend.position="none"), so there is no error.

this may be a bug.
probably labs(fill=NULL) should work and draw nothing, because
labs(fill="") draw something invisible.

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Denis Chabot

unread,
Apr 17, 2011, 9:12:23 AM4/17/11
to Kohske Takahashi, ggplot2
Thank you Kohske, this worked and I have posted your suggestion on the learnr site, although it appears that in 2009, the original code did work without this problem (or the author of learnr would not have been able to produce the article).

Denis

Denis Chabot

unread,
Apr 17, 2011, 2:29:47 PM4/17/11
to ggplot2
Hi,

I want a series of bar plots to describe the abundance of different prey categories in the diet of a fish, according to fish size.
I used to do this with R base graphics. Here is one example:
<http://dl.dropbox.com/u/612902/Diet%20plot%20base%20graphics.pdf>

I think I advantageously reproduced this with ggplot2:
http://dl.dropbox.com/u/612902/Diet%20plot%20ggplot2.pdf

One of the things I I like about the ggplot2 version is the fact that the axes values are not repeated for each graphic, which increases the surface of the plot that is available to show data.

I am very much a beginner with ggplot2 and I did not manage to control the distance between tick labels and the axis. In base graphics, I did this by changing the mar and mgp parameters:

par(oma=c(1.4,1.4,0,0)+0.1, mar=c(1.8, 1.8, 0.2, 0.2)+0.1, xpd=NA, mgp=c(1.8,0.5,0), cex.lab=1.2)

My first question is: am I right to think that mar and mgp have no effect whatsoever with ggplot2?

Secondly, from what I found in the ggplot2 user guide, I should have been able to change the position of the tick labels with hjust and vjust. I can get this to work when I do not do facetting, but it does not seem to have any effect with facetting.

For instance, the figure above was obtained with this code (which should work for anyone because the data set is on my DropBox, but replace the quartz() with windows() if you are on Windows):

library(ggplot2)

load(url("http://dl.dropbox.com/u/612902/%20Data%20for%20diet%20plot.Rda"))

quartz(w=10,h=7)
a <- ggplot(thedataB, aes(x = Length_Class, y = value, fill = PreyNames)) + opts(title = "Cod Diet") +
labs(x = "Cod length (cm)", y = "Fullness Index")
theme_set(theme_bw())
b = a + geom_bar(stat = "identity") + facet_wrap(~ PreyNames, ncol=5)
c = b + opts(legend.position = "none", axis.text.x = theme_text(hjust=0.5, vjust=0.5), axis.text.y = theme_text(hjust=0.5, vjust=0.5) )
c

I tried to get the x tick labels a bit higher, closer to the x axis using vjust = 0 (or -1 or -5 for that matter) but only hjust has an effect on the position of the x tick labels (i.e. axis values). Similarly, I would have brought the y tick labels a bit closer to the y axis with a xjust = 1, but here only vjust seems to have an effect.

What am I missing?

Sincerely,

Denis

Kohske Takahashi

unread,
Apr 17, 2011, 10:09:50 PM4/17/11
to Denis Chabot, ggplot2
Hi,

try this:

c + opts(axis.ticks.margin=unit(1, "mm"))

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Denis Chabot

unread,
Apr 18, 2011, 4:04:03 PM4/18/11
to Kohske Takahashi, ggplot2
Thanks again, Kohske,

I thought I looked at all possible "options" and found none for this behaviour, but I was wrong.

Still, is is a feature or a bug (or incomplete knowledge of ggplot2 on my part) that vjust does not work for x tick labels on a facetted plot, while it clearly works on non-facetted plots? And same for hjust on y tick labels?

Denis

Hadley Wickham

unread,
Apr 25, 2011, 12:04:27 PM4/25/11
to Denis Chabot, Kohske Takahashi, ggplot2
If you can provide a minimal reproducible example, I can add it to the bug list.
Hadley

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

Denis Chabot

unread,
Apr 27, 2011, 6:08:09 PM4/27/11
to ggplot2, Hadley Wickham
To the list;

In response to a post I made to the list a while back (starting this thread), Hadley asked me to give a reproducible example.

By trying simpler and simpler examples, I realized that my problems with using axis.text.x had nothing to do with the facetting aspect of my figures. Either I do not understand how to use axis.text.x, or its behaviour is erratic. So I thought it best to show this using a Sweave document to combine input and figures. And I use the first example from <https://github.com/hadley/ggplot2/wiki/Axis-Attributes>.

So here is the Sweave script along with the pdf produced. Let's just say that I ended the Sweave document with:
"Now I am really confused!".


Denis

sessionInfo()

R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8


attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.1 reshape_0.8.4 plyr_1.5.1   




Denis
positioning_axis_text.Rnw
positioning_axis_text.pdf

Ista Zahn

unread,
Apr 27, 2011, 6:57:30 PM4/27/11
to Denis Chabot, ggplot2, Hadley Wickham
Hi Denis,

The following represents my understanding, and could well be wrong.
I'm sure someone will quickly correct me if that is the case.

The basic misunderstanding here is that vjust and hjust are not
intended to (v)ertically ad(just) or (h)orizontially ad(just) the
text. They are designed to set the (v)ertical (just)ification and
(h)orizontal (just)ification. Basically, they are continuous analogs
of aligning text left, center, right, top, middle, bottom. If you read
the help file you will see that they should be set to values between 0
and 1. See ?theme_text for reference.

Now, in reality it seems you can use them to adjust the position, by
passing values smaller than 0 or larger than 1, but that doing so
results in weird things happening. I wouldn't get too hung up on that,
because it's simply not how they were designed to work. If anything,
theme_text could return an error if vjust or hjust were set outside
the [0,1] interval.

HTH,
Ista

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

> Le 2011-04-26 à 09:31, Hadley Wickham a écrit :
>
>
> The ultimate would be to reproduce with a dataset like:
>
> df <- data.frame(x = c("a", "b"), y = 1:2, z = 3:4)
>
> Thanks!
>
> Hadley
>
>
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Denis Chabot

unread,
Apr 27, 2011, 9:41:18 PM4/27/11
to Ista Zahn, ggplot2, Hadley Wickham
Hi Ista,

You may be right, but in this case the analogy with basic R's adj parameter would be lost because adj is intended to "justify" text, but by using values outside the 0-1 interval you can also move text.

This said, there are reasons to think we should be able to use values outside the 0-1 interval with predictable results. For instance, the value of 7 is used in Hadley's own example. Furthermore, if we could ONLY use values within 0 and 1 to have predictable results, then we'd need another parameter to be able to control how far the axis text is from the axis. Finally, in my tests, vjust had no effect at all unless I also used a non-zero value for angle.

My feeling is that hjust and vjust are supposed to do what I wanted, but something is wrong with the implementation right now. This would explain why the example from <https://github.com/hadley/ggplot2/wiki/Axis-Attributes> is not repeatable.

Cheers,

Denis

Ista Zahn

unread,
Apr 28, 2011, 12:01:56 AM4/28/11
to Denis Chabot, ggplot2, Hadley Wickham
I still think setting values outside [0,1] usually makes no sense, and
only serves to complicate things -- and the function is not documented
to work that way (and I don't think Hadley is responsible for the wiki
examples). I played around with it a bit, and the behavior of the
hjust and vjust makes intuitive sense to me, even though I can't quite
articulate the rules exactly. If you use labels longer than one
character it helps to see when and how things are justified. Then,
systematically change things so you can isolate each effect:

dat <- data.frame(x=c("Long label for X value one", "Long label for X
value 2"), y=c(100000, 100001))
p1 <- ggplot(dat, aes(x=x, y=y)) + geom_point()
p1

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=.5),
axis.text.y=theme_text(vjust=.5, hjust=.5))

p1 + opts(axis.text.x=theme_text(vjust=1, hjust=.5),
axis.text.y=theme_text(vjust=1, hjust=.5))

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=1),
axis.text.y=theme_text(vjust=.5, hjust=1))

p1 + opts(axis.text.x=theme_text(angle = 45),
axis.text.y=theme_text(angle = 45))

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=.5, angle = 45),
axis.text.y=theme_text(vjust=.5, hjust=.5, angle = 45))

p1 + opts(axis.text.x=theme_text(vjust=1, hjust=.5, angle = 45),
axis.text.y=theme_text(vjust=1, hjust=.5, angle = 45))

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=1, angle = 45),
axis.text.y=theme_text(vjust=.5, hjust=1, angle = 45))

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=.5, angle = 90),
axis.text.y=theme_text(vjust=.5, hjust=.5, angle = 90))

p1 + opts(axis.text.x=theme_text(vjust=1, hjust=.5, angle = 90),
axis.text.y=theme_text(vjust=1, hjust=.5, angle = 90))

p1 + opts(axis.text.x=theme_text(vjust=.5, hjust=1, angle = 90),
axis.text.y=theme_text(vjust=.5, hjust=1, angle = 90))

Wow, that was boring and lame. Sorry. Anyway, it makes sense to me.
Now what is is you want to do? What is the problem with the way the
theme_text settings work?

Best,
Ista

Reply all
Reply to author
Forward
0 new messages