0
votes

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

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.Jaap
Hi, thanks but my files are not splitter by id but by date… DO I have to do two for loops?user3036416
The myfiles contains the original data frame per date..user3036416

1 Answers

1
votes

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.