Felipe Carrillo
unread,Jun 19, 2012, 11:59:14 AM6/19/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ggplot2
Hi:
I am trying to put totals on each panel but the results are un-
readable, I would appreciate any advice
library(ggplot2)
library(plyr)
library(reshape)
library(scales)
w_sub <- structure(list(week = c(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), BY2003 = c(765L, 1753L, 6226L, 9727L, 22033L,
50472L, 67863L, 100909L, 276699L, 239609L, 416465L, 845073L,
1071051L, 571656L, 409021L, 451331L, 167006L, 68069L, 250439L,
104641L, 76062L, 35261L, 55883L, 16064L, 11058L, 8872L, 3633L,
3092L, 0L, 533L, 6766L, 411L, 672L, 2352L, 2352L, 3158L, 3604L,
1276L, 830L, 188L, 191L, 78L, 211L, 0L, 0L, 0L, 0L, 102L, 0L,
0L, 0L, 0L), BY2004 = c(0L, 147L, 1231L, 3216L, 5021L, 18462L,
37441L, 64669L, 136590L, 338523L, 559448L, 714837L, 358037L,
180910L, 291943L, 222708L, 163801L, 39109L, 23247L, 15726L, 38701L,
5795L, 12509L, 18721L, 822L, 6674L, 2494L, 4009L, 220L, 72L,
278L, 73L, 60L, 6135L, 989L, 507L, 172L, 201L, 388L, 211L, 392L,
106L, 88L, 341L, 232L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), BY2005 = c(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), BY2006 = c(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), BY2007 = c(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), BY2008 = c(0L, 0L, 132L,
227L, 2157L, 7558L, 16459L, 87220L, 321195L, 209299L, 110807L,
112024L, 128058L, 74373L, 41531L, 15111L, 21890L, 15060L, 22538L,
12443L, 11052L, 3907L, 2640L, 2745L, 3936L, 17593L, 906L, 558L,
930L, 956L, 62L, 379L, 2911L, 9548L, 4095L, 886L, 2604L, 36L,
237L, 248L, 0L, 70L, 273L, 314L, 134L, 40L, 0L, 0L, 0L, 0L, 0L,
0L), BY2009 = c(93L, 329L, 363L, 2762L, 5573L, 33466L, 96762L,
340947L, 612835L, 347972L, 526506L, 656981L, 344181L, 145465L,
150543L, 935599L, 32789L, 15092L, 7288L, 9889L, 6945L, 5138L,
6299L, 3095L, 108227L, 1191L, 194L, 6802L, 11570L, 4004L, 659L,
238L, 259L, 1186L, 861L, 566L, 1427L, 189L, 489L, 816L, 0L, 735L,
313L, 147L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), BY2010 = c(0L, 460L,
1129L, 1757L, 11105L, 27730L, 55769L, 12298L, 32964L, 80110L,
160507L, 74134L, 101736L, 138355L, 107873L, 53806L, 194429L,
7596L, 55497L, 30336L, 18392L, 22291L, 16850L, 16567L, 14799L,
7909L, 13807L, 3156L, 4451L, 4025L, 211L, 430L, 2121L, 1324L,
1820L, 2067L, 2027L, 0L, 0L, 1527L, 281L, 0L, 140L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), BY2011 = c(0L, 0L, 0L, 0L, 161L, 321L,
1000L, 4425L, 13202L, 19726L, 30518L, 84949L, 157260L, 145691L,
85801L, 62044L, 44439L, 23272L, 22391L, 20159L, 14854L, 35379L,
31142L, 7736L, 13221L, 4894L, 4129L, 1578L, 1175L, 8331L, 655L,
134L, 398L, 284L, 328L, 107L, 6803L, 351L, 652L, 202L, 42L, 48L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA)), .Names = c("week",
"BY2003", "BY2004", "BY2005", "BY2006", "BY2007", "BY2008", "BY2009",
"BY2010", "BY2011"), class = "data.frame", row.names = c(NA,
-52L))
# Select the last 7 years of data only
lastcol <- length(w_sub)
fromcol <- length(w_sub)-6
w_sub <- w_sub[,c(1,fromcol:lastcol)]
# Make a replica of dataset and melt it to get summed totals for each
panel
melted <- w_sub
total_m <- melt(melted,id='week');total_m
totals <-
ddply(total_m,"variable",summarise,total=sum(value,na.rm=TRUE));totals
names(totals) <- c('year','total');totals
# Using Dates for the X axis
w_sub$Date <- seq(as.Date("2008-07-1"),as.Date("2009-06-23"),by="7
days")
# Extract a date to use as a reference for the total summed value on
the plot area
# Is there a better way to do this?
xloc <- w_sub[44,9]
xloc
wintermelt <- melt(w_sub,id=c("week","Date"))
winter <- ggplot(wintermelt,aes(Date,as.numeric(value)/
1000,colour=variable)) +
geom_line(size=1) + theme_bw() + # overrides all colors and axis
formatting
scale_x_date(labels= date_format("%b"),breaks = "1 month",
minor_breaks = "2 week",expand=c(0,0)) +
#scale_x_date(major="month", minor="2 weeks",
format="%b",expand=c(0,0)) +
#scale_y_continuous(breaks=c(100,250,500,750,1000,1500,2000)) +
opts(title="Juvenile Winter Chinook Salmon Estimated
Passage",legend.position="none",
axis.text.x = theme_text(angle=45,hjust=1,size=8.2,colour="gray50"),
axis.text.y=theme_text(size=8.2,hjust=1,colour="grey50"),
axis.title.x=theme_text(size=9.0),axis.title.y=theme_text(size=9.0,angle=90))
+
facet_grid(variable~.) + labs(y="Number of individuals X 1,000") +
geom_text(aes(x=xloc,y=1500,label=total),data=totals,inherit.aes=FALSE)
winter