3

on a world map, how do I plot a single point?

all_states <- map_data("usa")
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group, legend = FALSE))
p

Also, is it possible to remove the grid and the lat long values form the map?

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • Please provide all the code, including libraries, you used to generate the error or sticking point. Also please include code in code tags using {code} or indenting every line of your code 4 spaces. – Tyler Rinker Feb 23 '12 at 22:57

1 Answers1

7
library(maps)
library(ggplot2)
world<-map_data('world')
sf<-data.frame(long=-122.26,lat=37.47)
p <- ggplot(legend=FALSE) +
geom_polygon( data=world, aes(x=long, y=lat,group=group)) +
opts(panel.background = theme_blank()) +
opts(panel.grid.major = theme_blank()) +
opts(panel.grid.minor = theme_blank()) +
opts(axis.text.x = theme_blank(),axis.text.y = theme_blank()) +
opts(axis.ticks = theme_blank()) +
xlab("") + ylab("")
# add a single point

p <- p + geom_point(data=sf,aes(long,lat),colour="green",size=4)
p

Note: Since version 0.9.2 opts has been replaced by theme. So for example, opts(panel.background = theme_blank()) would become theme(panel.background = element_blank()).

joran
  • 169,992
  • 32
  • 429
  • 468
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • 1
    nice, although (1) I like breaking long lines up so they be seen without scrolling horizontally and (2) it's probably best not to call the plot `plot` (the same as the name of the base-R plotting function) – Ben Bolker Feb 23 '12 at 23:01
  • Thanks @BenBolker I changed plot to p. – Maiasaura Feb 23 '12 at 23:19
  • 1
    Looks nice. There's one more "gotcha" here, though, which is that the `+` signs have to be at the *ends* of the split lines rather than the beginning of the continuation lines, unless you parenthesize the whole expression -- otherwise R will think the expression is complete ... – Ben Bolker Feb 23 '12 at 23:24
  • This plots a filled grey map. If you want just the outlines, use `geom_path()` – naught101 Aug 21 '12 at 07:26
  • Any ideas how to just plot the continent boundaries? – naught101 Aug 21 '12 at 07:42
  • I retrieved continent boundaries without country borders like this: `world <- fortify(as.data.frame(map('world', plot=FALSE, interior=FALSE)[c("x","y")])); names(world) <- c("long","lat")`. I'm sure there's an easier way... – naught101 Aug 21 '12 at 07:53