Histogram axis problems...

1,252 views
Skip to first unread message

j.bo...@hull.ac.uk

unread,
Jun 30, 2011, 12:52:17 PM6/30/11
to ggplot2
Hey everyone

With a little help from the group, I have produced a single column of
faceted histograms with ggplot2. Please see my script below. I have
removed all gridlines and background. However, I have some axis
formatting issues:

1) is it possible to change the frequency of major tick mark labels?
For example, "xlim(0, 460)" puts tick marks and labels every 100, I
would prefer every 25.
2) is it possible to insert minor tick marks (without labels) at the
same location as my binwidth, i.e. every 5?
3) the x axis is only plotted on the bottom facet. Is it possible to
plot the x axis and its tick marks on each facet?
4) there is a gap between the plotted data and the x and y axis. Is it
possible to remove this gap? For example, if using axis function in R:
base graphics, this would be achieved using "pos = 0"

Any help would be gratefully received!!!

Kind regards,

Jonathan



plotfunc <- function(site) {
ggplot(DATA[DATA$Site == site, ], aes(x = Length)) + xlim(0, 460)
+
geom_histogram(binwidth = 5) +
facet_grid(Survey.year ~ .) +
labs(x = "Length class (5-mm groups)", y = "Frequency") +
opts(axis.text.x = theme_text(angle = 90, family = "serif"),
axis.text.y = theme_text(family = "serif"), axis.title.x =
theme_text(family = "serif"), axis.title.y = theme_text(angle = 90,
family = "serif"), axis.line = theme_segment(), axis.ticks =
theme_segment(), panel.background = theme_blank(), panel.grid.minor =
theme_blank(), strip.text.y = theme_text(family = "serif"),
strip.background = theme_blank(), axis.text.y = theme_text(),
axis.text.x = theme_text())
ggsave(paste(site, ".wmf", sep=""), path = "C:/R", width = 6.2,
height = 8.7)
}

sitelist <- as.list(unique(DATA$Site))
l_ply(sitelist, .fun = plotfunc)

Luciano Selzer

unread,
Jun 30, 2011, 6:36:20 PM6/30/11
to j.bo...@hull.ac.uk, ggplot2
See inline for the anwsers
Luciano
HTH


Hey everyone

With a little help from the group, I have produced a single column of
faceted histograms with ggplot2. Please see my script below. I have
removed all gridlines and background. However, I have some axis
formatting issues:

1) is it possible to change the frequency of major tick mark labels?
For example, "xlim(0, 460)" puts tick marks and labels every 100, I
would prefer every 25.
Yes, change xlim() to scale_x_continuous(limits = c(0, 460), breaks = seq(0, 460, by =25)) 
2) is it possible to insert minor tick marks (without labels) at the
same location as my binwidth, i.e. every 5?
add minor_breaks =  seq(0, 460, by 5) inside of scale_x_continuous
3) the x axis is only plotted on the bottom facet. Is it possible to
plot the x axis and its tick marks on each facet?
No, is not possible
4) there is a gap between the plotted data and the x and y axis. Is it
possible to remove this gap? For example, if using axis function in R:
base graphics, this would be achieved using "pos = 0"
put expand = c(0,0) inside scale_y_continuous and scale_y_continuous. 

Any help would be gratefully received!!!

Kind regards,

Jonathan



plotfunc <- function(site) {
   ggplot(DATA[DATA$Site == site, ], aes(x = Length)) + xlim(0, 460)
+
   geom_histogram(binwidth = 5) +
   facet_grid(Survey.year ~ .) +
   labs(x = "Length class (5-mm groups)", y = "Frequency") +
   opts(axis.text.x = theme_text(angle = 90, family = "serif"),
axis.text.y = theme_text(family = "serif"), axis.title.x =
theme_text(family = "serif"), axis.title.y = theme_text(angle = 90,
family = "serif"), axis.line = theme_segment(), axis.ticks =
theme_segment(), panel.background = theme_blank(), panel.grid.minor =
theme_blank(), strip.text.y = theme_text(family = "serif"),
strip.background = theme_blank(), axis.text.y = theme_text(),
axis.text.x = theme_text())
 ggsave(paste(site, ".wmf", sep=""), path = "C:/R", width = 6.2,
height = 8.7)
}

sitelist <- as.list(unique(DATA$Site))
l_ply(sitelist, .fun = plotfunc)

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

Brian Diggs

unread,
Jul 1, 2011, 2:24:47 PM7/1/11
to Jon D Bolland, ggplot2
Replying back to list as well (although apparently my message didn't
make it to the list); see inline.

On 7/1/2011 2:57 AM, Jon D Bolland wrote:
> Dr Diggs
>
> Thank you for the reply and help!!!
>
> However, some of your advice hasn't worked. Your response to query #1
> worked fine. The code for query #4 worked, but the y axis disappears. I
> think it must be covered by the plot area, because if I slightly offset
> the y scale it is visible (i.e. "scale_x_continuous(limits = c(0,475),
> expand = c(0, 0.5))"). This is obviously a bit of a cheat, so if you
> know a different (more proper) way, please say?

I seem to remember something about this (the y axis being drawn under
the background). After some searching, I found the bug report:
https://github.com/hadley/ggplot2/issues/78

Maybe making panel.background=theme_rect(colour=NA, fill=NA) might work?
(rather than setting it to theme_blank?)

> For some reason, the script you suggested for drawing minor ticks didn't
> work (query #2). I have tried a number of different derivations,
> including using ticks instead of breaks (i.e. "minor_ticks = seq(0, 460,
> 5)") and also tried specifying the colour of minor ticks in opts (i.e.
> "axis.ticks.minor = theme_segment(colour = "black")"), based on the
> theory they might be drawn but without a colour. Any thoughts?

Oh, there are no minor tick marks, only minor grid lines (controlled by
panel.grid.minor, which you set to theme_blank)

> I am particularly confused by your response to query #3. In the ggplot2
> book there are faceted histograms drawn using qplot (my script uses
> ggplot and geom_histogram) on pages 118 and 121 that appear to have a
> black x axis below the data. Granted, this may not necessarily be the
> "axis". So, could a different R code produce the same result?

I looked at the graphs you mentioned, but didn't see an axis. What you
may be noting is that histograms are bars that go from 0 to whatever
value, so the bottom of all the bars line up along 0 giving a solid,
flat bottom. Though note that where there is no bar, there is not
bottom line. Is that what you are meaning?

> Any further thoughts and help would be greatly appreciated!!!
>
> Regards,
>
> Jonathan
>
> -----Original Message-----
> From: Brian Diggs [mailto:dig...@ohsu.edu]
> Sent: 30 June 2011 19:40
> To: j.bolland-Cu7FvJ...@public.gmane.org
> Cc: ggplot2
> Subject: Re: Histogram axis problems...
>
>
>
>
> On 6/30/2011 9:52 AM, j.bolland-Cu7FvJ...@public.gmane.org


> wrote:
>> Hey everyone
>>
>> With a little help from the group, I have produced a single column of
>> faceted histograms with ggplot2. Please see my script below. I have
>> removed all gridlines and background. However, I have some axis
>> formatting issues:
>

> Most of what you want to do can be done using scale_x_continuous
> directly rather than the helper function xlim. The xlim call itself
> would be replaced with scale_x_continuous(limits=c(0, 460))


>
>> 1) is it possible to change the frequency of major tick mark labels?
>> For example, "xlim(0, 460)" puts tick marks and labels every 100, I
>> would prefer every 25.
>

> That is the breaks argument. Add breaks=seq(0,460,25)


>
>> 2) is it possible to insert minor tick marks (without labels) at the
>> same location as my binwidth, i.e. every 5?
>

> That is the minor_breaks argument. Add minor_breaks=seq(0,460,5)


>
>> 3) the x axis is only plotted on the bottom facet. Is it possible to
>> plot the x axis and its tick marks on each facet?
>

> I don't believe so. An advantage of faceting is that the scale is the
> same so that you don't have to repeat it.


>
>> 4) there is a gap between the plotted data and the x and y axis. Is it
>> possible to remove this gap? For example, if using axis function in R:
>> base graphics, this would be achieved using "pos = 0"
>

> That is the expand argument. Add expand=c(0,0)


>
>> Any help would be gratefully received!!!
>

> To pull it all together, instead of xlim(0, 460) you would have
>
> scale_x_continuous(limits = c(0,460), breaks = seq(0, 460, 25),
> minor_breaks = seq(0, 460, 5), expand = c(0, 0))
>
> [Not completely tested since your example is not reproducible without
> DATA]


>
>> Kind regards,
>>
>> Jonathan
>>
>>
>>
>> plotfunc<- function(site) {
>> ggplot(DATA[DATA$Site == site, ], aes(x = Length)) + xlim(0, 460)
>> +
>> geom_histogram(binwidth = 5) +
>> facet_grid(Survey.year ~ .) +
>> labs(x = "Length class (5-mm groups)", y = "Frequency") +
>> opts(axis.text.x = theme_text(angle = 90, family = "serif"),
>> axis.text.y = theme_text(family = "serif"), axis.title.x =
>> theme_text(family = "serif"), axis.title.y = theme_text(angle = 90,
>> family = "serif"), axis.line = theme_segment(), axis.ticks =
>> theme_segment(), panel.background = theme_blank(), panel.grid.minor =
>> theme_blank(), strip.text.y = theme_text(family = "serif"),
>> strip.background = theme_blank(), axis.text.y = theme_text(),
>> axis.text.x = theme_text())
>> ggsave(paste(site, ".wmf", sep=""), path = "C:/R", width = 6.2,
>> height = 8.7)
>> }
>>
>> sitelist<- as.list(unique(DATA$Site))
>> l_ply(sitelist, .fun = plotfunc)
>>
>
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Jake Russ

unread,
Jul 2, 2011, 11:29:02 AM7/2/11
to ggplot2
> In the ggplot2
> book there are faceted histograms drawn using qplot (my script uses
> ggplot and geom_histogram) on pages 118 and 121 that appear to have a
> black x axis below the data. Granted, this may not necessarily be the
> "axis". So, could a different R code produce the same result?

I see what you mean. I've drawn those charts with the book's code on
pg 118 and it's particularly striking in the 1910 facet. Drawing those
plots now it looks like there should be a gap in the book's picture
(fig 7.3), but it looks connected as if by a line. I don't know of any
opt() that would allow you to add a line to the 'axis' of each facet.
What it feels like to me is that ggplot2 might have changed the way it
draws histograms between now and the time of the book.

Reply all
Reply to author
Forward
0 new messages