0

am making plots for multiple species of crabs. One plot for each species (first loop) and year (second loop). I want to add the year ('yy') to each plot in the title using paste0(). For some reason ggplot will not recognize the yy variable that I have specified prior to the ggplot() command. Any advice?

Unfortunately the data is a bit too large for me to share.

map_save <- data.frame(map('state' , region=c('maine', 'new hampshire', 'vermont', 'massachusetts', 'rhode island', 'connecticut',
                       'new york', 'pennsylvania','west virginia','delaware', 'new jersey', 'maryland', 'virginia', 'north carolina'),
    boundary=T,
    ylim=lat.range, xlim=lon.range,myborder=c(0,0),
    plot=FALSE)[c('x','y')])

for (ss in stocks){
   crabs_sub  <- crabs[crabs$SPP==ss & crabs$YEAR>=1982,]

  for (yy in years){
    crabs_yr <- crabs_sub[crabs_sub$YEAR==yy,] 
    dat <- data.frame(YEAR=crabs_yr[,'YEAR'],
                  Longitude=crabs_yr[,'LON'],Latitude=crabs_yr[,'LAT'],
                  biomass=crabs_yr[,'BIO'])

print( 
ggplot(map_save, aes(x=x, y=y)) + 
  labs(title=paste0(names[which(stocks==ss)],' - ',yy)) +
  geom_path() +
  coord_map(project = "mercator", xlim=lon.range,ylim=lat.range) +      
  geom_point(aes(x=Longitude,y=Latitude,size=log(biomass+1)),data=dat[,],alpha=1/2,shape=21,color='black',fill='black') +
      scale_size_continuous(name='Log(biomass+1)') +

  labs(x = "Longitude", y = "Latitude", size = 20) +

   )    
 }
}
struggleBus
  • 365
  • 2
  • 5
  • 20
  • 2
    Please take the time to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If the problem only happens in a loop, then it would he helpful if your example had a loop. Remove any code that's not relevant to the problem (is the `theme()` necessary?). Include sample input data so we can run the code and verify the problem. Also it looks like you have `pasteo` rather than `paste0`. If you are getting an error message, be sure to include the exact message in your question. – MrFlick Jul 22 '15 at 20:10
  • 1
    In the mean time, you could try `ggtitle(paste0(names[which(stocks==ss)],' - ',yy))` or `ggtitle(paste0(names[which(stocks=="ss")],' - ',yy))` – talat Jul 22 '15 at 20:11
  • ggtitle() works! Thank you! – struggleBus Jul 22 '15 at 20:22
  • ...or just add a `title` argument to the `labs` command you already have. (Pass it the same value as in docendo's comment.) – Gregor Thomas Jul 22 '15 at 20:56

0 Answers0