7

Map size with lat/long 38.31536111,-76.55011111 is different from map with lat/long 59.5624775,-139.7410994 (plotting points on the map)

while saving it with png()

How to keep the same size? height and width isn't enough?

EDIT: full code

library(maps)
library(ggplot2)
data <- read.csv("data.csv", header=TRUE)
lat = data$lat
long = data$long
world<-map_data('usa')
sf<-data.frame(long=long,lat=lat)
p <- ggplot(height=600, width=800) +
geom_polygon( data=world, aes(x=long, y=lat,group=group)) 
p <- p + geom_point(data=sf,aes(long,lat),colour="white",size=1)
p

data file:

"lat","long"
59.5624775,-139.7410994
42.38748056,-94.61803333

If I remove the first line in the data file, the map size is different (bigger), than when both lines are present

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149

1 Answers1

12

Your code is a bit of a mess mate, so I fixed it into reproducable format below. Solution is already given by @joran - you need to specify size in png().

library(maps)
library(ggplot2)
#specify size here
png("world.png",height=600,width=800)

#here is a way to create very simple data frame from you coordinates
data <- read.table(textConnection("
lat long 
59.5624775 -139.7410994 
42.38748056 -94.61803333"),header=TRUE,as.is=TRUE)
long=data$long
lat=data$lat
world <- map_data('usa')
sf<-data.frame(long=long,lat=lat)
ggplot() +
geom_polygon(data=world, aes(x=long, y=lat,group=group)) + 
geom_point(data=sf,aes(long,lat),colour="white",size=1)

#this saves png in your current directory
dev.off()

EDIT: Ups, now I made some error in the previous code, now it's fixed.

enter image description here

Geek On Acid
  • 6,330
  • 4
  • 44
  • 64
  • I generated two images, one with the first lat/long, and other with both lat/long, as per you code. The image sizes are still different. EDIT: I mean, the map sizes, the sizes of images are the same. –  Mar 03 '12 at 17:12
  • sorry about the messy code. Just learning R, and I should've cleaned it up before posting. –  Mar 03 '12 at 17:12
  • Never mind. I changed the lat/long in the sf<-data.frame line, and that fixed it. Thank you for your help :) –  Mar 03 '12 at 17:21