[R] help with x axis on lattice barchart--R-Beginner

Dennis Murphy djmuser at gmail.com
Thu Oct 20 01:54:42 CEST 2011


Hi:

Your example data don't exhibit the types of behavior that you're
concerned about if you keep it simple. Here's how you can get the
*example* data plotted in both lattice and ggplot2; if the example
data are a subset of a much larger data set, then see below.

This 'works' on the example data after reading in the observations:

# Define day as an ordered factor
chipps$day <- factor(chipps$Day, levels = c('1/10', '2/27', '5/30',
'6/5', '7/25', '12/7'))
library('ggplot2')
ggplot(chipps, aes(x = day, y = catch)) + theme_bw() + geom_bar(fill =
'red') + facet_wrap(~ year)

library('lattice')
chipps$year <- factor(chipps$year)
 barchart(Catch ~ Day| year, as.table = TRUE, col = 'red')

This assumes both year and Day are factors. The problem with your
definition of year.f and its subsequent use in barchart() is that it
contains year levels for which no data exists (specifically, 2005 and
2010). Using it as a conditioning factor in barchart() produces an
error:

barchart(chipps$catch ~ chipps$day | year.f, col = 'red')
Error in limits.and.aspect(default.prepanel, prepanel = prepanel,
have.xlim = have.xlim,  :
  need at least one panel

This is why I converted year to factor and used it as the conditioning
variable.

I don't know what your scales code was intended to do. A wild guess
would be that the 30 tick numbers are supposed to represent days of
the month, but the problem is that each panel represents a year. You
may want to think more carefully about how you want the dates
rendered.

Since there were only six unique dates within year, it was more
convenient to treat Day as an ordered factor rather than a Date
variable. It's less messy that way.

If your example data is much larger than this with many more
individual dates within year, one suggestion would be to create a Date
variable, but the problem then is to condition each panel by year and
to plot catch by month/day within panel. It's not clear to me how one
would easily do that in either lattice or ggplot2, so perhaps someone
else has a better idea and can suggest how one could do that with
dates.

HTH,
Dennis



On Wed, Oct 19, 2011 at 11:51 AM, Lauren Ledesma <lrledesma at gmail.com> wrote:
> Hi all,
>
> I am trying to compare catch vs day over a ten year period, and the graph
> looks great except for the individual dates. there are so many that my x
> axis labels have become one giant black line. Can I only label some of the
> days or will I be unable to fix this using a barchart?
>
> data is called chipps
>
> year.f<-factor(chipps$Year,levels=c(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011))
>  barchart(chipps$Catch~chipps$Day|year.f,layout=c(2,5),border="transparent",
> col=c("red"),scales=list(x=list(tick.number=30)))
>
> Day  year  catch
> 1/10 2001 1
> 1/10 2002 4
> 5/30 2002 5
> 2/27 2003 1
> 1/10 2004 3
> 5/30 2003 6
> 2/27 2006 3
> 6/5 2006 6
> 7/25 2006 7
>  5/30 2006 6
> 2/27 2009 2
> 1/10  2011 8
> 5/30 2004 3
> 2/27 2007 7
> 6/5 2008 5
> 12/7  2006 4
>
> Lauren L.
> Beginner in R
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list