2

I am quite new in R. I have number of coordinates and I want to plot them in a proper way in R which also presents labels. Moreover, axises should present the lat and long. I have tries ggplot but I cannot fit the data to the code.

   id      lon      lat
1   2 7.173500 45.86880
2   3 7.172540 45.86887
3   4 7.171636 45.86924
4   5 7.180180 45.87158
5   6 7.178070 45.87014
6   7 7.177229 45.86923
7   8 7.175240 45.86808
8   9 7.181409 45.87177
9  10 7.179299 45.87020
10 11 7.178359 45.87070
11 12 7.175189 45.86974
12 13 7.179379 45.87081
13 14 7.175509 45.86932
14 15 7.176839 45.86939
15 17 7.180990 45.87262
16 18 7.180150 45.87248
17 19 7.181220 45.87355
18 20 7.174910 45.86922
19 25 7.154970 45.87058
20 28 7.153399 45.86954
21 29 7.152649 45.86992
22 31 7.154419 45.87004
23 32 7.156099 45.86983
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Topdombili
  • 265
  • 4
  • 10
  • 1
    Can you present what you tried ? Also supply reproducible example - this might be helpful if you are using google map http://stackoverflow.com/questions/14095495/ploting-coordinates-of-multiple-points-at-google-map-in-r – shNIL Jan 03 '13 at 21:20
  • I have changed the lat o lon to spatial data and tried plot: > coordinates(data)= ~lon+lat and moreover, > plot(data$lon,data$lan,type="p", pch=22, lty=2, col="brown",xlab = "Lon" , ylab= "Lat" ) > plot(data) – Topdombili Jan 03 '13 at 21:23
  • If you want to use ggplot2, you cannot use the spatial data classes provided by the `sp` package, it only works on `data.frame`'s. To convert from spatial classes to data.frame, simply use `as.data.frame`, or don't convert them to spatial classes in the first place. – Paul Hiemstra Jan 03 '13 at 21:26
  • Strangely familiar data: http://stackoverflow.com/questions/14095495/ploting-coordinates-of-multiple-points-at-google-map-in-r – MattBagg Jan 03 '13 at 22:37
  • @MattBagg, Mr. Amidi intrduce me the site, Is there any problem with using a same dataset for you? – Topdombili Jan 03 '13 at 22:41
  • Not really, but you might learn more by asking for help with why you get specific error messages in intermediate steps rather than asking for how to do final solutions. – MattBagg Jan 03 '13 at 22:57

1 Answers1

6

To do this use the geom_text geometry:

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id))

This plots the text in the id column on the locations specfied by the columns lon and lat. The data is stored in the data.frame df.

or use:

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) + 
    geom_point()

if you want to add both a point and a label. Use the hjust and vjust parameters of geom_text to change the orientation of the label relative to the point. In addition, give each point a color according to the column var by using the color parameter in the geom_point aesthetics:

ggplot(aes(x = lon, y = lat), data = df) + geom_text(aes(label = id)) + 
    geom_point(aes(color = var))

Do note that ggplot2 cannot deal with the Spatial classes provided by the sp package. Use as.data.frame to convert point (SpatialPoints) and gridsets (SpatialPixels/SpatialGrid) to data.frame's. In addition, use fortify to convert polygon datasets (SpatialPolygons) to data.frame.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Is it possible to give each point a color and add them to legend? – Topdombili Jan 03 '13 at 21:37
  • 2
    ...added as an edit to the question. However, I recommend you read up on ggplot2 as these kinds of things can be found in the documentation of ggplot2. – Paul Hiemstra Jan 03 '13 at 21:48
  • I am reading it. I tried transparency for septate visualization of text and symbol by ggplot(aes(x = lon, y = lat), data = data) + geom_text(aes(label = id)) + geom_point(shape=23,alpha=3/4,size = 3, colour="gray",cex=.3) . But it doesn't give proper result. How can I change the position of the symbol, to the above or bottom? – Topdombili Jan 03 '13 at 22:04
  • For last update, I came up with this error: Don't know how to automatically pick scale for object of type function. Defaulting to continuous Error in data.frame(colour = function (x, y = NULL, na.rm = FALSE, use) : arguments imply differing number of rows: 0, 23 – Topdombili Jan 03 '13 at 22:04
  • 1
    Please add a reproducible example to your qeustion above. – Paul Hiemstra Jan 03 '13 at 22:09