如何在代码中保存R中的图形
我正在绘制观察到的和建模场的2个概率密度函数。 我在下面有此代码:
# Import libraries
library("stringr")
library(ggplot2)
#EXTENDED Observations with reanalysis obervatory 085320
# load in the text files and convert to dataframe
directory = '/Users/jacob/Desktop/Master Meteorologia/PE/Datos/Lisboa/wind/00_Observed/Extended/Observed-Wind-Lisboa_Extended/'
filename = '085320_Wind_Extended.txt'
directory2 = '/Users/jacob/Desktop/Master Meteorologia/PE/Datos/Lisboa/wind/MPI-ESM-MR/Historical/'
filename2 = 'Wind_MPI-ESM-MR_Historical_085320.txt'
extFile = read.delim(str_c(directory,filename), header = FALSE, col.names=c("Year", "Month", "Day", "WindMagn"))
histFile = read.delim(str_c(directory2,filename2), header = FALSE, col.names=c("Year", "Month", "Day", "WindMagn"))
# Extract vecto==wind from both cases
windMagnExt = extFile$WindMagn
windMagnHist = histFile$WindMagn
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
#png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', width = 1000, height=1000, units='px' ,res=700, pointsize = 12)
plot(d, main="PDF of observed and modelled wind for Obs. 085320", col="red")
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modelled"),
col=c("red", "blue"), lty=1:2, cex=0.8)
grid()
#dev.off()
它给了我这种类型的图:
在传说中,建模的字段是一条虚线,而不是。我该如何更改。
我尝试取消2行PNG()和dev.off()以将图保存在代码中,而不必在rstudio中使用导出按钮。我按照以下方式进行:
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', width = 15, height=10, units='px' ,res=700, pointsize = 12)
plot(d, main="PDF of observed and modelled wind for Obs. 085320", col="red")
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modelled"),
col=c("red", "blue"), lty=1:2, cex=0.8)
grid()
dev.off()
但是它给了我这个错误。我之前曾尝试过使用此此数字保存其他数字,但现在它告诉我,数字保证金太大
编辑:
基于DJ的评论,我在下面尝试运行png(),而无需运行png()参数:
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', res=300)
plot(d, main="PDF of observed and modeled wind for Obs. 085320", col="red", xlab='Wind speed (m/s)')
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modeled"),
col=c("red", "blue"), lty=1:1, cex=0.8)
grid()
dev.off()
它为我提供了我想要的绘图,但是当添加参数 res = 300 时,我仍然得到与上面相同的错误。
有帮助吗?谢谢!
I am plotting 2 probability density functions of observed and modeled fields.
I have this code below:
# Import libraries
library("stringr")
library(ggplot2)
#EXTENDED Observations with reanalysis obervatory 085320
# load in the text files and convert to dataframe
directory = '/Users/jacob/Desktop/Master Meteorologia/PE/Datos/Lisboa/wind/00_Observed/Extended/Observed-Wind-Lisboa_Extended/'
filename = '085320_Wind_Extended.txt'
directory2 = '/Users/jacob/Desktop/Master Meteorologia/PE/Datos/Lisboa/wind/MPI-ESM-MR/Historical/'
filename2 = 'Wind_MPI-ESM-MR_Historical_085320.txt'
extFile = read.delim(str_c(directory,filename), header = FALSE, col.names=c("Year", "Month", "Day", "WindMagn"))
histFile = read.delim(str_c(directory2,filename2), header = FALSE, col.names=c("Year", "Month", "Day", "WindMagn"))
# Extract vecto==wind from both cases
windMagnExt = extFile$WindMagn
windMagnHist = histFile$WindMagn
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
#png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', width = 1000, height=1000, units='px' ,res=700, pointsize = 12)
plot(d, main="PDF of observed and modelled wind for Obs. 085320", col="red")
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modelled"),
col=c("red", "blue"), lty=1:2, cex=0.8)
grid()
#dev.off()
It gives me this type of plot:
However, there are 2 things I still want to be done.
In the legend, the modelled field is a dashed line when it is not. How do I change this.
I try to uncomment 2 lines png() and dev.off() to save the figure within the code and not have to use the export button in RStudio. I do it as follows:
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', width = 15, height=10, units='px' ,res=700, pointsize = 12)
plot(d, main="PDF of observed and modelled wind for Obs. 085320", col="red")
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modelled"),
col=c("red", "blue"), lty=1:2, cex=0.8)
grid()
dev.off()
But then it gives me this error. I tried saving other figures using this before, but now it tells me that figure margins are too large
EDIT:
Based on D.J's comment below i tried running png() without any parameters:
### PDF observed and modelled
d= density(windMagnExt)
e= density(windMagnHist)
png('/Users/jacob/Desktop/Master Meteorologia/PE/Figures/windMagnObsHist_PDF.png', res=300)
plot(d, main="PDF of observed and modeled wind for Obs. 085320", col="red", xlab='Wind speed (m/s)')
lines(e, col="blue" )
legend("topright", legend=c("Observed", "Modeled"),
col=c("red", "blue"), lty=1:1, cex=0.8)
grid()
dev.off()
It gives me the plot I want but, when adding the parameter res=300, I still get the same error as above.
Any help? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论