[英]How to fix the size of a ggplot in R while saving it with png()?
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) 地图尺寸与纬线/长38.31536111,-76.55011111不同于地图lat / long 59.5624775,-139.7410994(地图上的绘图点)
while saving it with png() 用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 如果我删除数据文件中的第一行,则地图大小不同(更大),而不是两条线都存在时
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()
. 解决方案已由@joran提供 - 您需要在
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. 编辑:Ups,现在我在前面的代码中犯了一些错误,现在已经修复了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.