如何在R中使用png()保存ggplot的大小? [英] How to fix the size of a ggplot in R while saving it with png()?

查看:1717
本文介绍了如何在R中使用png()保存ggplot的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地图尺寸为经纬度38.31536111,-76.55011111
不同于地图
,经纬度为59.5624775,-139.7410994(在地图上绘制点)



用png保存()



如何保持相同的大小?高度和宽度不够?



编辑:完整代码

  library(maps)
library(ggplot2)
data < - read.csv(data.csv,header = TRUE)
lat = data $ lat
long =数据$ 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), color =white,size = 1)
p

数据文件:

 lat,long
59.5624775,-139.7410994
42.38748056,-94.61803333

如果我删除数据文件中的第一行,则地图大小不同(较大)您的代码是一个混乱的队友,所以我将它固定为下面的可重现格式。解决方案已由@joran提供 - 您需要在 png()中指定大小。

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

#有一种方法可以从您的坐标
data-read.table(textConnection(
lat long
59.5624775 -139.7410994
42.38748056)创建非常简单的数据框架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),color =white,size = 1)

#this将png保存在当前目录中
dev.off ()

编辑:Ups,现在我在前面的代码中犯了一些错误,现在它是固定的。 / p>


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

解决方案

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.

这篇关于如何在R中使用png()保存ggplot的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆