0

I'm plotting data in R. I'm running the following two commands:

plot(x = df$Latitude, df$Longitude, col = heat.colors(nrow(df)), type = "p")

plot(x = df$Latitude, df$Longitude, col = df$feature, type = "p")

The first line plots the points along a color gradient (points with higher values are red, points with lower values are yellow) and the second line plots data with color dictated by the int values given by features.

However, I want to combine both such that I'm plotting points with colors on a scale using the numeric values from feature. In some sense, I want to pass two arguments to col. How can I do this?

zthomas.nc
  • 3,689
  • 8
  • 35
  • 49

1 Answers1

1

You can try:

# some data
set.seed(123)
x <- rnorm(100)

# Create some breaks and use colorRampPalette to transform the breaks into a color code
gr <- .bincode(x, seq(min(x), max(x), len=length(x)), include.lowest = T)
col <- colorRampPalette(c("red", "white", "blue"))(length(x))[gr]

# the plot:
plot(x, pch=16, col=col)

enter image description here

For a legend see solutions here or here

Community
  • 1
  • 1
Roman
  • 17,008
  • 3
  • 36
  • 49