RandyHuang
RandyHuang

Reputation: 31

how to add color to the points to denote the values of another variable

library(maps)
map("state","California")
points(map$x,map$y)

I use the command above created a map with points that represent the houses location. how can I add colors to these points on the map to denote the values of another variable Z(which represent house value for example) ?

Upvotes: 1

Views: 1174

Answers (1)

thelatemail
thelatemail

Reputation: 93813

library(maps)
map("state","California")

Make some test data. I have changed the data name from map to mappts so it doesn't clash with the base function. It's not a good idea to name data the same name as a function.

dput(mappts)
structure(list(x = c(-121.837504273717, -119.288648121568, -116.37566966197
), y = c(40.0189660554, 36.8188807085794, 34.5400320525101)), .Names = c("x", 
"y"), row.names = c(NA, -3L), class = "data.frame")
mappts$z <- c(1,2,3)

> mappts
          x        y
1 -121.8375 40.01897
2 -119.2886 36.81888
3 -116.3757 34.54003

Add a z column

mappts$z <- c(1,2,3)
points(mappts,col=mappts$z,pch=19)

take me down to the paradise city

If your z column is not grouped so neatly, you may need to recode it first.

If you would like to specify your colours manually, you can edit the palette by doing:

palette(c("blue","pink","green"))

...which you can then reset using:

palette("default")

Upvotes: 4

Related Questions