1
\$\begingroup\$

I am an absolute newcomer in R. For my bachelor thesis I worked with spatial data. I did many plots with the normal "points"-function. I already have my results, but would like to improve my code.

This is the first example: (leg and colours were defined previously. WLPoi is a SpatialPointsDataFrame, german_reich_polyline_new is a SpatialLinesDataFrame and GEPolnew is a SpatialPolygonDataFrame. All of them have the same projection.)

SelNegativbelege <- (WLPoi$lfp_bra < 1) & (WLPoi$lfp_ack_se < 1) & (WLPoi$lfp_ack_na < 1) & (WLPoi$lfp_bau < 1) & (WLPoi$lfp_fel < 1) & (WLPoi$lfp_pfl_zu < 1) & (WLPoi$lfp_zu_na < 1) & (WLPoi$lfp_grab < 1) & (WLPoi$lfp_hack < 1) & (WLPoi$lfp_pfl_na < 1) & (WLPoi$lfp_pfl < 1) 
SelHack <- (WLPoi$lfp_hack > 0) 
SelGrab <- (WLPoi$lfp_grab > 0) 
... 

plot(GEPolnew, fill= NA) 
points(WLPoi[SelNegativbelege,], col=colormint, cex=.3, pch=16) 
points(WLPoi[SelHack,], col=colorolive, pch=16, cex=1) 
points(WLPoi[SelGrab,], col=colorgold, pch=16, cex=1) … 
lines(german_reich_polyline_new, lwd=2) legend("bottomright", leg, cex=0.55, bty="n",pch=19, col=c(colormint,colormaroon, colorolive, colorgold, colorred, c7, colortomato))

Maybe someone has also an advice how I can shorten:

EUnew_merge$lfp_bra100 <- ((EUnew_merge$lfp_bra)*100) 
EUnew_merge$lfp_ack_se100 <- ((EUnew_merge$lfp_ack_se)*100) 
EUnew_merge$lfp_ack_na100 <- ((EUnew_merge$lfp_ack_na)*100) 
EUnew_merge$lfp_bau100 <- ((EUnew_merge$lfp_bau)*100) 
EUnew_merge$lfp_fel100 <- ((EUnew_merge$lfp_fel)*100) 
EUnew_merge$lfp_pfl_zu100 <- ((EUnew_merge$lfp_pfl_zu)*100) 
EUnew_merge$lfp_zu_na100 <- ((EUnew_merge$lfp_zu_na)*100) 
EUnew_merge$lfp_grab100 <- ((EUnew_merge$lfp_grab)*100) 
EUnew_merge$lfp_hack100 <- ((EUnew_merge$lfp_hack)*100) 
EUnew_merge$lfp_pfl_na100 <- ((EUnew_merge$lfp_pfl_na)*100) 
EUnew_merge$lfp_pfl100 <- ((EUnew_merge$lfp_pfl)*100) 
EUnew_merge$Pflug100 <- ((EUnew_merge$Pflug)*100)

Or

selac0 <- (GEPolnew_merge$PFLUGNF4AC == 0) 
selac1 <- (GEPolnew_merge$PFLUGNF4AC > 0) 
selac2 <- (GEPolnew_merge$PFLUGNF4AC > 0.02) 
selac3 <- (GEPolnew_merge$PFLUGNF4AC > 0.05) 
selac4 <- (GEPolnew_merge$PFLUGNF4AC > 0.1) 
selac5 <- (GEPolnew_merge$PFLUGNF4AC > 0.2) 
selac6 <- (GEPolnew_merge$PFLUGNF4AC > 0.5) 
selac7 <- (GEPolnew_merge$PFLUGNF4AC > 0.8) 
selac8 <- (GEPolnew_merge$PFLUGNF4AC == 1)

or

plot(GEPolnew_merge, fill=NA) plot(GEPolnew_merge[selac0,], col=c1, add= TRUE) plot(GEPolnew_merge[selac1,], col=c2, add= TRUE) 
plot(GEPolnew_merge[selac2,], col=c3, add= TRUE) 
plot(GEPolnew_merge[selac3,], col=c4, add= TRUE) 
plot(GEPolnew_merge[selac4,], col=c5, add= TRUE) 
plot(GEPolnew_merge[selac5,], col=c6, add= TRUE) 
plot(GEPolnew_merge[selac6,], col=c7, add= TRUE) 
plot(GEPolnew_merge[selac7,], col=c8, add= TRUE) 
plot(GEPolnew_merge[selac8,], col=c9, add= TRUE) 
lines(german_reich_polyline_new, lwd=3) 
legend("bottomright", legc3, cex=1.15, bty="n",pch=c(1,19,19,19,19,19,19,19,19,19), col=c(rgb(0,0,0),c1,c2,c3,c4,c5,c6,c7,c8,c9))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.