Hi:

I don't know if this is exactly what you wanted, but here goes. I made
a few adjustments in the data frame before calling ggplot():

# library(ggplot2)

# Reorient the order in which variables appear
winter <- winter[, c(1, 7, 3, 6, 4, 5, 2)]

# Get rid of second week 26 at the end
winter2 <- winter[-53, ]

# Create an ordered factor for labeling the weeks
wkord <- c(26:52, 1:25)
winter2$week <- ordered(winter2$week, levels = wkord)

# melt the data frame
w <- melt(winter2, id = 'week')

# create ordered factor for year
w$year <- substring(as.character(w$variable), 3, 6)
w$year <- factor(w$year, levels = c(2005, 2009, 2006, 2008, 2007, 2010),
                          ordered = TRUE)

# x variable in ggplot() must be continuous for geom_line() to work
g <- ggplot(w,aes(x = wk, y = value/1000, colour = year))

# Plot with week numbers on x-axis, rotated and resized for presentation
g + geom_line() + scale_x_continuous(breaks = 1:52, labels = levels(w$week))
+
 opts(axis.text.x = theme_text(angle = 90, vjust = 0.5, hjust = 1, size =
7))

HTH,
Dennis

On Mon, Aug 30, 2010 at 2:25 PM, Felipe Carrillo
<mazatlanmex...@yahoo.com>wrote:

> Please consider the following dataset:
> I want to reorder the levels by year but get the following error:
>
> Error in tapply(v, x, FUN, ...) : arguments must have same length
>
> I suspect that I need to add the levels before I melt the dataset
>  but either way I have only use 'reorder' once before and can't figure
>  out how it works..Thanks for any advice.
>
> winter <- (list(week = c(26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
> 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
> 47L, 48L, 49L, 50L, 51L, 52L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
> 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
> 22L, 23L, 24L, 25L, 26L), BY2010 = c(0L, 0L, 460L, 1126L, 1755L,
> 11153L, 27998L, 56336L, 12486L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L, 0L), BY2009 = c(0L, 95L, 341L, 373L, 2859L, 5769L,
> 33263L, 98435L, 345339L, 622621L, 349958L, 531232L, 652803L,
> 345358L, 142991L, 148883L, 957501L, 32495L, 14862L, 7210L, 9765L,
> 6846L, 5067L, 6201L, 3045L, 106230L, 1183L, 195L, 6855L, 10261L,
> 4179L, 650L, 240L, 165L, 1189L, 863L, 562L, 1350L, 188L, 479L,
> 770L, 0L, 558L, 314L, 147L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
>     BY2008 = c(0L, 0L, 77L, 124L, 159L, 2376L, 9480L, 17314L,
>     99574L, 323679L, 198211L, 93630L, 129183L, 111820L, 71260L,
>     35241L, 14020L, 20778L, 21694L, 15016L, 13400L, 9187L, 3607L,
>     2804L, 2417L, 5291L, 16216L, 898L, 558L, 709L, 972L, 61L,
>     372L, 3086L, 10108L, 4295L, 882L, 2593L, 36L, 233L, 243L,
>     0L, 70L, 272L, 308L, 134L, 40L, 0L, 0L, 0L, 0L, 0L, 0L),
>     BY2007 = c(0L, 0L, 0L, 10775L, 4166L, 4958L, 16221L, 29401L,
>     34951L, 33188L, 146044L, 105007L, 185297L, 159682L, 207537L,
>     140694L, 128275L, 44274L, 27079L, 18928L, 10437L, 29984L,
>     18395L, 25846L, 4573L, 31995L, 3679L, 1043L, 9636L, 1524L,
>     827L, 7009L, 233L, 433L, 0L, 1103L, 257L, 128L, 66L, 70L,
>     535L, 429L, 97L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
>     BY2006 = c(0L, 707L, 2390L, 8860L, 24430L, 40885L, 72792L,
>     205521L, 344493L, 662973L, 526409L, 631953L, 850491L, 842678L,
>     445987L, 558152L, 332032L, 174326L, 80601L, 48696L, 98571L,
>     103563L, 149469L, 78081L, 182478L, 2158L, 16566L, 4027L,
>     2655L, 1112L, 567L, 2595L, 4976L, 6336L, 294L, 1758L, 291L,
>     203L, 450L, 1098L, 788L, 195L, 532L, 0L, 0L, 0L, 0L, 0L,
>     0L, 167L, 0L, 0L, 0L), BY2005 = c(0L, 0L, 868L, 2044L, 4064L,
>     6049L, 9399L, 13304L, 45172L, 242155L, 476864L, 712534L,
>     1058409L, 2115018L, 1510342L, 1138213L, 333192L, 158820L,
>     94379L, 348882L, 39290L, 29701L, 47258L, 69837L, 7884L, 49338L,
>     22168L, 19397L, 19397L, 15984L, 8688L, 1200L, 1623L, 1291L,
>     1356L, 707L, 875L, 875L, 222L, 883L, 0L, 0L, 129L, 0L, 0L,
>     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("week", "BY2010",
> "BY2009", "BY2008", "BY2007", "BY2006", "BY2005"), class = "data.frame",
> row.names = c(NA,
> -53L))
>  str(winter)
>  w_melt <- melt(winter,id="week",variable="year");str(w_melt)
>  #  Reorder DOESN'T WORK
>  w_melt <-
>
> reorder(w_melt$year,c("BY2005","BY2009","BY2006","BY2008","BY2007","BY2010"))
> pdf("wtest.pdf")
> ggplot(w_melt,aes(week,value/1000,colour=year,order= - as.numeric(year))) +
> geom_line(size=.75)+  theme_bw() +
> opts(title="Cumulative",axis.text.x = theme_text(angle=45,hjust=1)) +
>  labs(y="Number of individuals X 1,000",x="week")
> dev.off()
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
>
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to