我有几个ggplots作为我的ls上的对象.我想将它们保存为单独的文件(虽然我也有兴趣知道如何将它们全部保存在1个大文件中).我读过这个:问题和问题,但我似乎无法适应代码.我也尝试将它们全部放在一个大文件中,如此处所示,但确实得到了这个错误:Error in do.call("grid.arrange", plots2[[i]]) : second argument must be a list.在一个列表中获取所有ggplots时,我遗漏了一些东西.
这是我到目前为止所尝试的:
> ls() #List of objects on my ls. All the p* are my ggplots that I want to save.
[1] "all" "dat" "dat2" "dat3" "data" "dlook" "dlook2" "dlook3" "i" "look2" "mdfx"
[12] "objects" "order" "p" "p1" "p10" "p11" "p12" "p13" "p14" "p15" "p16"
[23] "p17" "p18" "p19" "p2" "p3" "p4" "p5" "p6" "p7" "p8" "p9"
> objects<-ls()
> plot<-objects[14:30]
> …Run Code Online (Sandbox Code Playgroud) 我一直试图在ggplot2上绘制世界地图.我跟着电子邮件的主题:ggplot map with l但是我遇到了同样的错误信息,我不明白作者对如何修复它的评论.
library(rgdal)
library(ggplot2)
library(maptools)
library(sp)
gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")
> world.ggmap <- fortify(world.map, region = "NAME")
Error in nchar(ID) : invalid multibyte string 1
Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个阴谋,并希望得到任何帮助.我试图在我的geom_points上绘制一个多边形.这是我到目前为止所做的:
> names(OT1)# my dataset
[1] "EID" "latitude" "longitude" "month" "year" "CPUE" "TSUM"
> dim(OT1)
[1] 2707 7
> head(OT1)
EID latitude longitude month year CPUE TSUM
1 167-1-1996-1135 67.70000 -61.81667 9 1996 0 0
2 167-10-1996-1135 67.71667 -59.18333 9 1996 0 0
3 167-100-1996-1135 67.86667 -59.43333 10 1996 0 0
4 167-101-1996-1135 67.95000 -59.58333 10 1996 0 0
5 167-102-1996-1135 68.10000 -59.76667 10 1996 0 0
6 167-103-1996-1135 67.81667 -59.38333 10 1996 0 0
OTz<-OT1[with(OT1,OT1$TSUM=="0"),]#selecting only my zeros
OTc<-OT1[!with(OT1,OT1$TSUM=="0"),] …Run Code Online (Sandbox Code Playgroud) 我用来能够毫无问题地运行这个脚本,但是现在fortify {ggplot2}命令给了我一个错误信息.任何可能是问题的暗示都会很棒!我使用fortify命令能够使用ggplot2对shapefile进行geom_map.
下面是我下载数据的脚本和链接.
#######################################################
#######################################################
rm(list = ls(all = TRUE))#clear workspace
getwd()
#upload packages
library(maps)
library(mapdata)
library(gridExtra)
library(rgdal)
library(rgeos)
library(ggplot2)
library(sp)
library(maptools)
gpclibPermit()
#setwd(".../FAO") FAO data are major fishing area divisions
> FAO<- readOGR(dsn="fao", layer="World_Fao_Zones")
OGR data source with driver: ESRI Shapefile
Source: "fao", layer: "World_Fao_Zones"
with 19 features and 1 fields
Feature type: wkbPolygon with 2 dimensions
> names(FAO);dim(FAO)
[1] "zone"
[1] 19 1
> str(FAO,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 19 obs. …Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题.我是使用ts数据和所有相关R包的新手.我有一个带有几个变量的df,包括GMT"%H%M"中的"一天中的时间"和日期"%Y /%m /%e"采样.我想将我的日期数据分箱/汇总到"周"(即%W /%g),并计算在该周内进行抽样时的平均'当天时间'.
我能够通过首先将我的df转换为zoo对象然后使用aggregate.zoo命令来计算数值变量(例如,权重)的其他FUN,如下所示:
#calculate the sum weight captured every week
x2c <- aggregate(OA_zoo, as.Date(cut(time(OA_zoo), "week")), sum)
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何解决这个事实,即我正在使用Date格式而不是num,并且会感谢任何提示!此外,我显然已经通过单独执行每个变量来编写方法.是否有办法通过使用plyr聚合"每周"来对我的df应用不同的FUN(sum/mean/max/min)?还是其他一些套餐?
编辑/澄清
这dput是我的完整数据集样本的输出.我有2004 - 2011年的数据.我想看一下/使用ggplot2进行绘图是指随着时间的推移(2004-2011)累计的TIME(%H%M)的平均值/中位数.现在,我的数据不会在一周内汇总,而是每天汇总(随机抽样).
> dput(godin)
structure(list(depth = c(878, 1200, 1170, 936, 942, 964, 951,
953, 911, 969, 960, 987, 991, 997, 1024, 978, 1024, 951, 984,
931, 1006, 929, 973, 986, 935, 989, 1042, 1015, 914, 984), duration = c(0.8,
2.6, 6.5, 3.2, 4.1, 6.4, 7.2, 5.3, 7.4, 7, 7, 5.5, 7.5, …Run Code Online (Sandbox Code Playgroud) 我有以下数据框(下面),它是我的完整数据集的子集.我需要为具有相同LAT/LONG和着陆日期的每个条目定义相同的ID.我之前订购的LANDEDDATE从最早到最晚.
> dput(df2)
structure(list(LATITUDE = c(43.35, 43.35, 43.35, 43.35, 43.35,
43.35, 43.35, 43.35, 43.5166, 43.5166, 43.5166, 43.5166, 43.5166,
43.5166, 43.5166, 42.9833, 42.9833, 42.9833, 42.9666, 42.9666
), LONGITUDE = c(-60.6163, -60.6165, -60.7167, -60.7166, -60.7163,
-60.716, -60.7169, -60.7166, -59.9169, -59.9168, -59.9169, -59.9166,
-59.9166, -59.916, -59.916, -61.8333, -61.8333, -61.8333, -61.9161,
-61.9161), LANDEDDATE = structure(c(11171, 11171, 11183, 11183,
11183, 11183, 11183, 11183, 11192, 11192, 11192, 11192, 11192,
11192, 11192, 11210, 11210, 11210, 11210, 11210), class = "Date"),
sppCODE = c(251L, 251L, 251L, 251L, 251L, …Run Code Online (Sandbox Code Playgroud) 我正在尝试用geom_raster绘制光栅图像(海洋测深),但我很难打破代码.我将不胜感激任何帮助.
我已经下载了以下xyz文件(Dropbox链接).
SF.bath <- read.table("SF_bath.txt", header=F,col.names=c("lon","lat","z"))
> head(SF.bath)
lon lat z
1 -67.9917 50.9968 757
2 -67.9750 50.9968 693
3 -67.9583 50.9968 673
4 -67.9417 50.9968 769
5 -67.9250 50.9968 761
6 -67.9084 50.9968 733
Run Code Online (Sandbox Code Playgroud)
当我尝试以下代码时,颜色不会显示在我的地图上?
v <- ggplot(SF.bath, aes(lon, lat,z=z))
print(v + geom_raster(aes(fill=z)) + stat_contour(size=0.2) + scale_fill_gradient("z"))
Run Code Online (Sandbox Code Playgroud)

我收到以下消息:Using alpha as value column: use value_var to override.有什么可能是问题的想法?
我正在尝试在循环中创建点模式列表ppp.object {spatstat}.我的数据集如下所示:
> names(OT1);head(OT1);dim(OT1)
[1] "EID" "latitude" "longitude" "month" "year" "CPUE" "TSUM"
[8] "fTSUM"
EID latitude longitude month year CPUE TSUM fTSUM
1 167-1-1996-1135 67.70000 -61.81667 9 1996 0 0 F
2 167-10-1996-1135 67.71667 -59.18333 9 1996 0 0 F
3 167-100-1996-1135 67.86667 -59.43333 10 1996 0 0 F
4 167-101-1996-1135 67.95000 -59.58333 10 1996 0 0 F
5 167-102-1996-1135 68.10000 -59.76667 10 1996 0 0 F
6 167-103-1996-1135 67.81667 -59.38333 10 1996 0 0 F
[1] 2707 8
Run Code Online (Sandbox Code Playgroud)
我想做的是为我的每个月选择数据并创建一个ppp.object. …
我正在尝试使用ggplot2重现与此地图类似的内容:
到目前为止,这是我所做的:
load("mapdata.Rdata")
> ls() #2 datasets: "depth" for basemap (geom_contour) and "data" is use to construct geom_bin2d
[1] "data" "depth"
> head(data)
latitude longitude GRcounts
740 67.20000 -57.83333 0
741 67.11667 -57.80000 0
742 67.10000 -57.93333 1
743 67.06667 -57.80000 0
751 67.15000 -58.15000 0
762 67.18333 -58.15000 0
ggplot(data=data,aes(x =longitude, y =latitude))
+theme_bw()
+ stat_bin2d(binwidth = c(0.5, 0.5))
+geom_contour(data=depth,aes(lon, lat, z=dn),colour = "black", bins=5)
+ xlim(c(-67,-56)) + ylim(c(65,71))
Run Code Online (Sandbox Code Playgroud)
这给了我这张地图: 
最后一步是在我的geom_bin2d圆上显示,其大小与每个bin中的计数之和(Grcounts)成正比。
最好在ggplot中提供有关如何执行此操作的任何技巧。
后续问题:使用facet_wrap时stat_bin2d和stat_summary2d之间的对齐方式不匹配
当我在diamonds数据集上运行以下代码时,没有明显的问题:
但是,如果我对数据运行相同的代码,则会出现对齐问题。关于什么可能导致此问题的任何想法?
p<-ggplot(diamonds,aes(x =carat, …Run Code Online (Sandbox Code Playgroud) r ×9
ggplot2 ×6
aggregate ×1
dataframe ×1
dictionary ×1
geospatial ×1
image ×1
loops ×1
map ×1
save ×1
shapefile ×1
sorting ×1
time-series ×1
variables ×1