0

I need to plot line with discrete color for each segment component give the following data frame:

head(profilo1, 10)
   dx TPI      DEM
1   0   5 2159.219
2   2   5 2157.735
3   4   5 2156.269
4   6   5 2154.752
5   8   5 2153.282
6  10   5 2151.925
7  12   5 2150.660
8  14   3 2149.402
9  16   3 2148.277
10 18   2 2147.149

dx & DEM are x and y, while TPI is the color attribute (span from 1:10) I need to link to color. I wish to use some color table from RColorBrewer (eg. Paired). While I'm looking around for solution, I find useful this

color_paired = brewer.pal(n = 10, "Paired")

plot(profilo1$dx, profilo1$DEM,t='p', 
     xlab = "Profile distance (m)", 
     ylab = "Elevation (m.s.l.m)",
     col = color_paired[profilo1$TPI], cex= .3)

But I'll prefer line to point.

lmo
  • 37,904
  • 9
  • 56
  • 69

1 Answers1

0

You can get this by first making a blank plot and then using the segements function.

library(RColorBrewer)
color_paired=brewer.pal(n = 10, "Paired")

plot(profilo1$dx,profilo1$DEM, xlab="Profile distance (m)",
     ylab="Elevation (m.s.l.m)", type="n")
segments(head(profilo1$dx, -1), head(profilo1$DEM, -1), 
    profilo1$dx[-1], profilo1$DEM[-1], 
    col=color_paired[head(profilo1$TPI, -1)])

Colored segments

Data

profilo1 = read.table(text="dx TPI      DEM
1   0   5 2159.219
2   2   5 2157.735
3   4   5 2156.269
4   6   5 2154.752
5   8   5 2153.282
6  10   5 2151.925
7  12   5 2150.660
8  14   3 2149.402
9  16   3 2148.277
10 18   2 2147.149",
header=TRUE)
G5W
  • 36,531
  • 10
  • 47
  • 80