我在将.shp(esri形状文件)读入R中时遇到了困难.我在R中尝试了几个选项,并尝试将ArcMap中的形状文件转换为正确读取形状文件但尚未生效的内容.(在ArcMap中,我更正了几何体,从单个多边形转换为多边形等,这可能不是必需的或相关的)
它可能与我的形状文件包含'区域'(多边形)而不是'多边形'这一事实有关...
如何在R中正确读取该类型的形状文件以进行绘图?(它看起来像ArcMap中的普通形状)
在ArcMap中,形状文件如下所示:http: //bfast.r-forge.r-project.org/arcmapshape.jpg
(显示其他多边形内包含多边形的形状文件)
在R中它看起来像这样:

(显示错误填充某些多边形的形状文件)
我在R中使用了以下代码:
require(maptools)
require(rgdal)
newproj <- "+proj=utm +zone=55 +south +ellps=GRS80 +units=m"
shape<- readShapeSpatial(pdir, proj4string = CRS(newproj),repair=TRUE,force_ring=T,verbose=TRUE) # without any errors
plot(shape, col="gray",border="blue", axes=TRUE)
# via rgdal
folder <- c("spatial")
lyr <- c("clipped_forest_mga")
shp <- readOGR(dsn=folder,layer=lyr)
plot(shp, col="gray",border="blue", axes=TRUE)
Run Code Online (Sandbox Code Playgroud)
两个plot()命令都给出相同的R结果.没有错误发生.只有以下消息
OGR data source with driver: ESRI Shapefile
Source: "P:/Victoria_DSE/BFAST_spatial/vector/PLM_excl_fire03_09_GDA94LL/mgaz94z55/clipped_EG", layer: "clipped_forest_mga"
with 1 features and 4 fields
Feature type: wkbMultiPolygon with 2 dimensions
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题?
将不规则时间序列转换为常规时间序列时遇到问题.下面是一个简化的例子:
require(zoo)
t <- as.character(c(1981,1984,1985))
d <- c(1,3,6)
dt <- data.frame(d,t)
t <- as.Date(t,"%Y")
z <- zoo(d,t)
plot(z)
ts.d <- as.ts(as.zooreg(z,freq=1)) # create a regular ts object
ts.d # regular time series
Run Code Online (Sandbox Code Playgroud)
我想创建一个看起来像这个c(1981,NA,NA,1984,1985)的常规时间序列ts.d.
令人惊奇的是,我第一次运行它:它的工作原理!但是当我想再次运行它或重复它(as.ts()行)它会停止工作并且我获得了很长的时间序列:
ts.d # regular time series
Time Series:
Start = 4299
End = 5760
Frequency = 1
[1] 1 NA NA NA NA NA NA NA NA NA NA NA NA NA
[15] NA NA NA NA NA NA NA NA
Run Code Online (Sandbox Code Playgroud)
等等
出了什么问题?
我正在尝试通过以下命令将95 GB CSV文件上传到MySQL数据库(MySQL 5.1.36):
CREATE TABLE MOD13Q1 (
rid INT UNSIGNED NOT NULL AUTO_INCREMENT,
gid MEDIUMINT(6) UNSIGNED NOT NULL ,
yr SMALLINT(4) UNSIGNED NOT NULL ,
dyyr SMALLINT(4) UNSIGNED NOT NULL ,
ndvi DECIMAL(7,4) NOT NULL comment 'NA value is 9',
reliability TINYINT(4) NOT NULL comment 'NA value is 9',
ndviquality1 TINYINT(1) NOT NULL ,
ndviquality2 TINYINT(1) NOT NULL ,
primary key (rid),
key(gid)
) ENGINE = MyISAM ;
LOAD DATA INFILE 'datafile.csv' INTO TABLE MOD13Q1 FIELDS …Run Code Online (Sandbox Code Playgroud) 我发布这个问题是为了询问如何在Windows XP机器上优化R中多个处理器的使用.
目前我正在创建4个脚本(每个脚本用例如for(i in 1:100)和(i in 101:200)等),我同时在4个不同的R会话中运行.这似乎使用了所有可用的cpu.
但是,我希望这样做更有效率.一种解决方案可能是使用"doMC"和"foreach"软件包,但这在Windows机器上的R中是不可能的.
例如
library("foreach")
library("strucchange")
library("doMC") # would this be possible on a windows machine?
registerDoMC(2) # for a computer with two cores (processors)
## Nile data with one breakpoint: the annual flows drop in 1898
## because the first Ashwan dam was built
data("Nile")
plot(Nile)
## F statistics indicate one breakpoint
fs.nile <- Fstats(Nile ~ 1)
plot(fs.nile)
breakpoints(fs.nile) # , hpc = "foreach" --> It would be great to test this.
lines(breakpoints(fs.nile))
Run Code Online (Sandbox Code Playgroud)
任何解决方案或建议?
与spplot()图例相比,如何使用ggplot改进空间栅格地图的图例?
我想使用ggplot()而不是ssplot()绘制空间地图,但是与spplot相比,我想提高一些方面:
## load packages
require(raster)
require(ggplot2)
require(rgdal)
require(RColorBrewer)
set.seed(1)
r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40,
crs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100
+ellps=WGS84")
r <- setValues(r,matrix(rnorm(1600, mean=0.4,sd=0.2)))
## 1. spatial map with spplot
cuts <-seq(minValue(r),maxValue(r),length.out=8)
cuts = round(cuts,digits=2)
col.regions = brewer.pal(length(cuts)+3-1, "RdYlGn")
print(
spplot(as(r, 'SpatialGridDataFrame'),at=cuts,
col.regions=col.regions,
colorkey=list(labels=list(at=cuts),at=cuts), pretty=TRUE,
scales=list(draw=T)
)
)
## 2. spatial map with ggplot
p = rasterToPoints(r); df = data.frame(p)
colnames(df) = c("x", "y", "NDVI")
p <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + …Run Code Online (Sandbox Code Playgroud) 我想用TikZ包创建一个简单的乳胶流程图,类似于下面的例子
http://www.texample.net/tikz/examples/simple-flow-chart/
但是我想在流程图中包含数字(在R中创建的时间序列图,如eps或其他)(例如在{block}中?
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [block] (init) {initialize model};
\node [cloud, left of=init] (expert) {expert};
\node [cloud, right of=init] (system) {system};
\node [block, below …Run Code Online (Sandbox Code Playgroud) 我在R中有以下问题.我想从不规则的时间序列(即日期和数据值列表)创建一个ts()对象(即一个常规时间序列).
您可以使用以下数据集和R脚本重现该问题:
# dput(dd) result
dd <- structure(list(NDVI = structure(c(14L, 4L, 11L, 12L, 20L, 17L,
5L, 7L, 21L, 23L, 25L, 19L, 15L, 9L, 3L, 24L, 2L, 6L, 22L, 16L,
13L, 18L, 10L, 8L, 1L), .Names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25"), .Label = c("0.4186",
"0.5452", "0.5915", "0.5956", "0.6010", "0.6860", "0.6966", "0.7159",
"0.7161", "0.7264", "0.7281", "0.7523", "0.7542", "0.7701", "0.7751",
"0.7810", "0.7933", …Run Code Online (Sandbox Code Playgroud)