I have a SpatialLinesDataFrame
dataset of rivers in R. The dataset consists of lines (river segments) with a "Turbidity" attribute. I want to create a map and legend, where:
- Turbidity between 0 and 10 is yellow
- Turbidity between 10 and 20 is orange
- Turbidity between 20 and 30 is red
Here is my sample dataset:
library(sp)
#create the sample SpatialLines with 3 lines and 1 attribute
x1 <- c(15.10,15.20, 15.25)
y1 <- c(48.9, 49.1, 49.0)
Line1 <- Line(cbind(x1, y1))
LineString1 <- Lines(list(Line1), ID=1)
x2 <- c(15.25,15.21)
y2 <- c(49.0, 49.12)
Line2 <- Line(cbind(x2, y2))
LineString2 <- Lines(list(Line2), ID=2)
x3 <- c(15.21,15.12)
y3 <- c(49.12, 49.05)
Line3 <- Line(cbind(x3, y3))
LineString3 <- Lines(list(Line3), ID=3)
spLines <- SpatialLines(list(LineString1, LineString2, LineString3))
spLinesDF <- SpatialLinesDataFrame(spLines, data.frame(Turbidity=c(5, 24, 16), row.names=c(1, 2, 3)))
#this is the color breaks that I want to use in map and legend
breaks = c(0, 10, 20, 30)
colors = c("yellow", "orange", "red")
#this doesn't assign the correct colors to my line values
plot(spLinesDF, col=colors)
What commands do I use to match the color breaks (0, 10, 20, 30) with the colors(yellow, orange, red) and with the Turbidity attribute values in my map and in the legend?
I know that for raster I could use simple commands like:
brk <- c(0, 10, 20, 30)
col <- c('yellow', 'orange', 'red')
plot(raster, col=col, breaks=brk)
But for SpatialLines, the plot
doesn't appear to support the color breaks parameter.