Open splboyle opened 8 years ago
Hey Adel - another for loop question.
I'm not sure if I'm doing to correctly.. This is what I've got.
preds<-c("meanWV", "SD_SH_DIFF","HARD_CORAL", "MA", "CCA", "DEPTH")
for (i in unique(preds)){ d <- subset(wsd, REEF_ZONE == "Forereef" & meanWV > 0) print(ggplot(d, aes(x = preds[i], y = TotFish)) + geom_point() + facet_wrap(~ISLAND)) }
But I keep getting this error..
Error in if (zero_range(from) || zero_range(to)) { : missing value where TRUE/FALSE needed
Someday it will make sense! Thanks for all your help!
Hi Shannon - Try aes_string() instead of aes - I expect it isn't finding the x properly. a
I tried aes_string but I then get an error saying TotFish is not found..
Try using the preds pointer you create in the subset statement? Then the x, y can come straight off from d as before.
The preds[i] pointer? Like this?
for (i in unique(preds)){ d <- subset(wsd, preds[i] & REEF_ZONE == "Forereef" & meanWV > 0) print(ggplot(d, aes(x = preds[i], TotFish)) + geom_point() + facet_wrap(~ISLAND)) }
still returns that same error.
Changed tactics slightly and tried this:
func <- function(x, na.nm = T){ nm <- names(x)[c(16:20,27,1345)] for(i in seq_along(nm)){ p <- ggplot(wsd, aes(x = nm[i], y = TotFish))+ geom_point() + geom_smooth(method="lm", formula=y~x) + facet_wrap(~ISLAND) print(p) } }
func(wsd)
Which returns graphs but they're not pulling the values within each predictor column.
for(i in (wsd)[c(16:20,27,1345)]){ p <- ggplot(wsd, aes(x = i, y = TotFish))+ geom_point() + geom_smooth(method="lm", formula=y~x) + scale_y_log10() + facet_wrap(~ISLAND) print(p) }
This kind of works but not entirely. It now recognizes the data within the columns but isn't correctly identifying all of the different predictors.. When I try to save the plots they are incorrect.
I'm also not entirely sure how to best include the REEFZONE, etc. subset.
Hey Adel - I've just about figured out plotting in for loops but I'm struggling with visualizing the plots for each island and/or saving them within the loop. So far I have this:
for (i in unique(wsd_1$ISLAND)){ d <- subset(wsd_1, ISLAND == i & REEF_ZONE == "Forereef" & meanWV > 0) wave <- ggplot(d, aes(meanWV, TotFish)) + geom_point() + geom_smooth(method="lm", formula=y~x) + labs(title = paste("Mean Wave Energy (2002-2012) and \n Total Fish Biomass at", i), x = "Mean Wave Energy", y = "Total Fish Biomass (g/m-2)") + ggsave(wave,filename=paste("wave_tot",i,".png",sep="_")) }
It would be nice to either add a facet to visualize them all at once (which I also can't entirely figure out) or simply save them as I'm trying to do here. However, this for some reason isn't working and I'm not sure where I've gone wrong. I get this error:
Saving 8.94 x 6.78 in image Error in grid.draw(plot) : object 'wave' not found
and don't totally understand why. Any tips?
Thanks for the notes you just sent as well!