How to fix ggplot size in R while saving it using png ()?

The size of the card with lat / long 38.31536111, -76.55011111 differs from the card with lat / long 59.5624775, -139.7410994 (drawing points on the map)

saving it with png ()

How to keep the same size? height and width is not 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 delete the first line in the data file, the map size is different (larger) than with both lines

+6
source share
1 answer

Your code is a bit messy, so I installed it in the reproducible format below. The solution is already given by @joran - you need to specify the 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 mistake in the previous code, now it is fixed.

enter image description here

+10
source

Source: https://habr.com/ru/post/909885/


All Articles