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