是否有可能在R中创建一个没有连续数据的三维轮廓图? [英] Is it possible to create a 3d contour plot without continuous data in R?
问题描述
我想用x,y,z数据创建变量z的轮廓。然而,似乎我们需要按递增顺序提供数据。
我试着使用一些代码,但它给了我错误。
我尝试了以下代码:试用1:
age2100 < - read.table( temp.csv,header = TRUE,sep =,)
x< - age2100 $ x
y< - age2100 $ y
z< - age2100 $ z
contour(x,y,z,add = TRUE,col =black)
我得到以下错误:
contour.default中的错误(x,y,z,add = TRUE,col =black):增加预期的'x'和'y'值
然后尝试使用ggplot2创建轮廓。我使用了下面的代码:
library(ggplot2)
library(MASS)
库(rgdal)
库(gpclib)
库(maptools)
age2100< - read.table(temp.csv,header = TRUE,sep = ,)
v < - ggplot(age2100,aes(age2100 $ x,age2100 $ y,z = age2100 $ z))+ geom_contour()
v
我收到以下错误消息:
警告消息:
无法生成轮廓数据
请在以下位置):
source(image.scale.R)# http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html
x11(width = 5,height = 6)
布局(矩阵(c(1,2),nrow = 1,ncol = 2),widths = c(4,1),height = 6,respect = TRUE)
layout.show(2)
par(mar = c(4,4,1,1))
image(fld)
contour(fld,add = TRUE)
points(age2100 $ x,age2100 $ y ,bch =。,cex = 2)
par(mar = c(4,0,1,4))
image.scale(fld $ z,xlab = ,ylab =,xaxt =n,yaxt =n,horiz = FALSE)
box()
axis(4)
mtext(text,side = 4 ,line = 2.5)
I want to create a contour of variable z with the x,y,z data. However, it seems like we need to provide the data in increasing order.
I tried to use some code but it gave me the error.
I tried the following code: Trial 1:
age2100 <- read.table("temp.csv",header=TRUE,sep=",") x <- age2100$x y <- age2100$y z <- age2100$z contour(x,y,z,add=TRUE,col="black")
I got the following error
Error in contour.default(x, y, z, add = TRUE, col = "black") : increasing 'x' and 'y' values expected
I then tried to use ggplot2 to create the contour. I used the following code:
library("ggplot2") library("MASS") library("rgdal") library("gpclib") library("maptools") age2100 <- read.table("temp.csv",header=TRUE,sep=",") v <- ggplot(age2100, aes(age2100$x, age2100$y,z=age2100$z))+geom_contour() v
I got the following error:
Warning message:
Not possible to generate contour data
Please find the data on the following location https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv
Can anybody tell me how to create the contour data from the third variable (z) from the temp.csv ? I need to do these many times so I am trying to do on R instead of Arcgis.
解决方案Here is an example of how one interpolates using
interp
from theakima
package:age2100 <- read.table("temp.csv",header=TRUE,sep=",") x <- age2100$x y <- age2100$y z <- age2100$z require(akima) fld <- interp(x,y,z) par(mar=c(5,5,1,1)) filled.contour(fld)
Here is an alternate plot using the
image
function (this allows some flexibility to adding lower level plotting functions (requires theimage.scale
function, found here):source("image.scale.R") # http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html x11(width=5, height=6) layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(4,1), height=6, respect=TRUE) layout.show(2) par(mar=c(4,4,1,1)) image(fld) contour(fld, add=TRUE) points(age2100$x,age2100$y, pch=".", cex=2) par(mar=c(4,0,1,4)) image.scale(fld$z, xlab="", ylab="", xaxt="n", yaxt="n", horiz=FALSE) box() axis(4) mtext("text", side=4, line=2.5)
这篇关于是否有可能在R中创建一个没有连续数据的三维轮廓图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!