0

Hi I have many data frame like this

       id     oldid   yr mo dy    lon    lat
1  01206295 Aberfeldy 1885  3 22 -127.1 -31.78
2  05670001  05670005 1885  3 22  -4.38  49.15
3     06279     06279 1885  3 22 -123.5   37.5
4    106251     06323 1885  3 22  178.5   19.5
5 58FFF3618 58FFF3618 1885  3 22  -0.73  69.73
6 Achille.F Achille.F 1885  3 22 -35.62  -2.98

stored in different files myfiles and I am trying to plot the (lon,lat) points for each of them with the colour chosen according to the id value. So far I am doing like this

for (i in 1:length(myfiles)){
      colnames(myfilesContent[[i]]) <-c("id","oldid","yr","mo","dy","lon","lat")
      p <- ggplot() + geom_polygon(data=world_map,aes(x=long, y=lat,group=group)) 
      myfilesContent[[i]]$lon <- as.numeric(myfilesContent[[i]]$lon)
      myfilesContent[[i]]$lat <- as.numeric(myfilesContent[[i]]$lat) 
      p + geom_point(data=myfilesContent[[i]], aes(x=lon, y=lat, fill=as.factor(id)), size = 4, shape = 21, show_guide=FALSE)
      print(p)
}

Anyway I am not sure that if an id is in different files it will be assigned with the same colour

Many thanks

3
  • 1
    It depends on how you split the dataframe. If you split it by id (like I did in my previous answer to your question), then you shouldn't have a problem. Commented May 27, 2014 at 11:32
  • Hi, thanks but my files are not splitter by id but by date… DO I have to do two for loops? Commented May 27, 2014 at 11:40
  • The myfiles contains the original data frame per date.. Commented May 27, 2014 at 11:55

1 Answer 1

1

You can make sure the levels for all your id columns are the same. First, get a master list of all the IDs from all the data.frames

allids <- unique(unlist(lapply(myfilesContent, function(x) levels(x[,1])))

Then make sure all the ID columns share these levels

lapply(seq_along(myfilesContent), function(i) {
    myfilesContent[[i]][,1] < -factor(myfilesContent[[i]][,1], levels=allids)
})

If they have the same levels, they should get the same colors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.