Two "stupid" questions: 1. how to make the bars start from the axis in a bar chart? 2. how to change the tick mark labels in a barchart?

2,017 views
Skip to first unread message

Jone Wy

unread,
Oct 2, 2009, 2:40:16 AM10/2/09
to ggplot2
Hi, all,

I bought the new ggplot2 book and studied extensively. I think the
ggplot2 is the most promising graphic package in
R because of the grammer underlying it. However, the books makes me
confused sometimes.

First, all the examples, especially those bar charts, the bars started
not from the axis. This is easilly seen when
I changed the theme to the all black-white traditional looks. The fact
that the bars are distanced from the X axis make it looks strange.
Also, I could not change the tick mark labels in a barchart. The breaks
() and labels() functions does not work for me in a barchart.

Code:
temp1<-mo1.tc2.0005$contributions
mo1.rel.inf<-data.frame(temp1)
a1<-ggplot(mo1.rel.inf,aes(var,rel.inf),stat="identity")+geom_bar()
+ylim(0,80)
> mo1.rel.inf
var rel.inf
1 Complementarity 70.2882460
2 H 25.7461915
3 Ecosystems 3.5824652
4 Origin 0.3830973
So when I plot this by the following, I got the strange results, in
which the bars are in the order of Complementarity, Ecosystems, H, and
Origin.

So I did the following: a1<-ggplot(mo1.rel.inf,aes(sort
(var,increasing=TRUE),rel.inf),stat="identity")+geom_bar()+ylim(0,80)

This time, the y values are correct according to the data, but the
tick mark labels are not in correct order.
So I tried to change the tick mark labels by hand, but failed.
I solved this by doing the following:

mo1.rel.inf$fvar<-factor(seq(1:4),levels=c(1,2,3,4),labels=c
("Complementarity","H","Ecosystems","Origin"))
a1<-ggplot(mo1.rel.inf,aes(fvar,rel.inf),stat="identity")+geom_bar()
+ylim(0,80)

Thank you million times if anyone here could help me with this.

Dan
Graduate Student
Forestry and environmental sciences
Lakehead University
Ontario, Canada

Jone Wy

unread,
Oct 2, 2009, 2:55:14 AM10/2/09
to ggplot2
A complain about ggplot2, the ggplot2 works wonders at the default set-
up, but I have not seen any similar graphic styles,i.e.,gray
background with substantial grid lines in most of the top ecological
journals. It is quite difficult and verbose for beginners to
effectively alter the output to the traditional black and white graph.

baptiste auguie

unread,
Oct 2, 2009, 3:02:00 AM10/2/09
to Jone Wy, ggplot2
Have you tried using themes instead of opts()?

e.g.

qplot(1,1) + theme_bw()

and you can define your own theme.

HTH,

baptiste

Jone Wy

unread,
Oct 2, 2009, 3:12:26 AM10/2/09
to ggplot2
OK. I found out that the labels could be changed by scale_x_discrete
(labels=c("aaa","bbb","ccc","ddd")) in the bar chart, which
contradicts the descriptions in the book "If labels is set, you must
also specify breaks"-page 97.

However, I still need to move the bars right at the x axis. Otherwise,
my supervisor would not accept the graph as "publication quality".
Please help me. Thank you very very much.

Jone Wy

unread,
Oct 2, 2009, 3:20:06 AM10/2/09
to ggplot2

My question one (the distance between bars and X axis) could be
demonstrated by the examples in Fig. 8.5 in ggplot2 book(page 146).

This is my first time posting here. If I break the forum rules in any
way, please let me know politely.

baptiste auguie

unread,
Oct 2, 2009, 3:40:33 AM10/2/09
to Jone Wy, ggplot2
Could you post a minimal reproducible example (i.e. including data,
see ?dput) that demonstrates your problems?

baptiste

On Fri, Oct 2, 2009 at 9:26 AM, Jone Wy <rinec...@gmail.com> wrote:
> Thank you baptiste, I tried that. But I find some arguments did not
> work at theme_bw(). I used theme_set(theme_bw()). But this may be due
> to that fact that I am a beginner in both R and ggplot2.
>
> Now I am more concernd about the positions of the axis. Any insight
> would be much appreciated.

>> > effectively alter the output to the traditional black and white graph.- Hide quoted text -
>>
>> - Show quoted text -

baptiste auguie

unread,
Oct 2, 2009, 3:59:37 AM10/2/09
to Jone Wy, ggplot2
It just occurred to me that you may be talking about the expand
parameter in the y-scale,

hgram <- qplot(rating, data = movies, binwidth = 1)
hgram + theme_bw() + scale_y_continuous(expand=c(0,0))

Does this help?

baptiste

hadley wickham

unread,
Oct 2, 2009, 9:17:12 AM10/2/09
to Jone Wy, ggplot2

I have tuned ggplot2 to have (in my opinion) to be aesthetically
appealing and perceptually well sound - the demands on the top
ecological journals did come into play ;) As Baptiste mentioned, if
you have particular needs, you can create your own theme.

Hadley

--
http://had.co.nz/

hadley wickham

unread,
Oct 2, 2009, 9:20:23 AM10/2/09
to Jone Wy, ggplot2
> First, all the examples, especially those bar charts, the bars started
> not from the axis. This is easilly seen when
> I changed the theme to the all black-white traditional looks. The fact
> that the bars are distanced from the X axis make it looks strange.

No, the bars start from the line y = 0, which is where you'd expect.
It's a really bad idea to put the axes right on the limits of the data
because then they overlap!

> Code:
> temp1<-mo1.tc2.0005$contributions
> mo1.rel.inf<-data.frame(temp1)
> a1<-ggplot(mo1.rel.inf,aes(var,rel.inf),stat="identity")+geom_bar()
> +ylim(0,80)
>> mo1.rel.inf
>              var              rel.inf
> 1 Complementarity   70.2882460
> 2               H             25.7461915
> 3      Ecosystems     3.5824652
> 4          Origin             0.3830973

Judging from that output, there is something weird going on with your
variables - is var is a factor?

Hadley


--
http://had.co.nz/

baptiste auguie

unread,
Oct 2, 2009, 10:09:39 AM10/2/09
to ggplot2
(Oops, meant to send the reply to the list too.)


On Fri, Oct 2, 2009 at 3:55 PM, Jone Wy <rinec...@gmail.com> wrote:
> yes. the expand=c(0,0) is doing the work. Thanks a lot. I will reconsider
> the thought of eliminating any of the space between axis line and geom(bars)
> after Hadley's comment though.
>

I personally agree with the idea that having the data touch the axes
is a bad idea. However I know some people won't tolerate axes that
don't meet, or extend further than the data (especially when the data
are bound between 0 and 1), or the absence of a thick box frame around
the plot.

A compromise I found was to add horizontal lines (or vertical) with
annotate() at, say, 0 and 1. In your case that would be an horizontal
line at the base of the bars. If not too obtrusive in colour or
thickness, Tufte might not call it junk.

Reply all
Reply to author
Forward
0 new messages