An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050413/fdcbb18a/attachment.pl
How to plot Contour with NA in dataframe
3 messages · WeiQiang.Li@seagate.com, Duncan Murdoch, Earl F. Glynn
WeiQiang.Li at seagate.com wrote:
Dear friends,
I am trying to produce Contour Plot with R, but there are some NA
in my data matrix. After I ran the following R script, I got the error
message:"no proper `z' matrix specified". Does anybody know how to plot
contour chart with R for the non-strict matrix?
Thank you in advance!!!
myData<-read.table('C:/MyDoc/TestData.txt',sep=',')
x <- 10*1:nrow(myData)
y <- 10*1:ncol(myData)
filled.contour(x, y, myData, color = terrain.colors,
plot.title = title(main = "The Topography of Maunga Whau",
xlab = "Meters North", ylab = "Meters West"),
plot.axes = { axis(1, seq(100, 800, by = 100))
axis(2, seq(100, 600, by = 100)) },
key.title = title(main="Height\n(meters)"),
key.axes = axis(4, seq(4, 8, by = 1)))
C:/myDoc/TestData.txt is shown as below:
4.95,,5.6250,5.66666666666666666667,5.90,5.80,5.50,5.70,5.0250,5.90,5.7250,5.75,5.70,5.30,,6,5.50,5.65,,5.7750,5.70,4.90,6.05,,5.4750,5.85,5.55,,,5.8250,5.65,5.75,,5.5750,5.4750,5.3750,5.8750,5.9250,5.55,6.3750,5.70,5.78333333333333333333,,5.55,,,,,
,5.5250,4.85,,,,6.15,5.86666666666666666667,,5.20,5.5750,5.74,5.76666666666666666667,6.73333333333333333333,6.05,5.88333333333333333333,5.80,5.8750,,6.15,,5.6250,5.5375,5.65,6,5.8250,5.55,5.85,5.65,5.89,6.15,5.60,,5.65,5.7250,,5.25,6.25,,5.56666666666666666667,5.9750,6.0250,,,6.35,,,,
5.45,,6.30,5.71666666666666666667,,5.50,6.05,5.83333333333333333333,5.58333333333333333333,5.6250,5.70,5.9250,5.7750,6.20,5.53333333333333333333,5.6625,5.40,5.75,,6,5.53333333333333333333,,5.6250,5.55,5.65,5.55,5.70,,6.15,5.90,5.71666666666666666667,6.11666666666666666667,5.15,6.10,5.4750,,5.75,5.45,5.95,,5.95,5.36666666666666666667,,,,6.15,,,
,5.7250,5.75,6,,,5.65,,6.30,5.65,,5.4750,5.60,5.41666666666666666667,5.8250,,,6.25,6.30,,5.60,6.15,6.10,5.85,,5.8750,5.65,5.9750,5.80,5.65,5.90,6.15,5.56666666666666666667,5.75,,,6,5.85,5.75,5.7250,5.75,,,,,,,,
5.75,,6.11666666666666666667,,5.3250,,5.91666666666666666667,5.55,,6.05,5.70,5.7250,5.50,6.15,5.40,5.40,5.40,5.70,5.4250,5.7250,5.45,,,5.85,5.85,5.7750,5.8750,5.25,5.45,5.75,5.70,5.65,5.4750,5.7250,5.70,6.10,6.05,5.6875,5.60,,5.80,,,5.65,,,,,
5.6250,6.25,5.60,5.90,6.0750,5.80,5.45,,,,,5.60,5.50,,,,,5.7375,5.60,6,5.96666666666666666667,5.68333333333333333333,5.90,5.83333333333333333333,5.6750,,5.6750,5.7250,5.68333333333333333333,5.63333333333333333333,5.65,6.05,,5.57,5.75,5.2750,5.50,,5.6750,5.85,5.6750,6.35,,,,,,5.35,
,5.20,,5.58333333333333333333,5.83333333333333333333,5.9250,5.7750,5.66666666666666666667,5.85,5.95,,6.35,5.55,5.3750,,6.30,5.15,5.40,5.55,,5.75,5.45,6.60,,5.45,5.25,,5.25,5.80,5.45,4.80,,5.4250,5.7750,5.15,5.55,5.35,,5.85,5.76666666666666666667,,,,,,,5.60,,
,,,5.15,,6.05,5.43333333333333333333,5.70,,5.75,6.05,5.15,5.75,5.60,,6.1750,,6.05,,,,5.5625,5.41666666666666666667,,5.6250,5.50,5.90,5.95,5.90,6.0250,5.96666666666666666667,6,5.70,6,5.90,6,5.55,5.81666666666666666667,5.7750,5.30,,5.7750,,,,5.40,,,
,6.06666666666666666667,5.45,5.60,5.55,5.95,5.8750,5.8750,5.80,5.91666666666666666667,,,,5.85,5.75,5.51666666666666666667,,5.8750,,5.45,6.0250,,5.80,5.9375,5.7750,5.51666666666666666667,,5.8750,5.60,5.8625,5.63333333333333333333,,5.50,5.2625,5.65,,5.30,5.35,,5.7250,,5.83333333333333333333,5.85,5.70,,5.30,,,
5.7750,6,5.55,,5.45,5.4125,5.99166666666666666667,5.50,5.65,5.9750,,,5.76666666666666666667,5.83333333333333333333,5.79,5.85,,5.90,5.98333333333333333333,6.05,,6.3250,6.0750,,5.25,5.85,6.20,,5.45,,,5.65,5.7250,5.9250,6.50,5.35,5.95,,5.88333333333333333333,5.80,5.95,5.60,5.75,,5.90,6.45,,,
5.68333333333333333333,5.60,,5.55,5.1250,,6.05,5.75,5.75,5.68333333333333333333,6,5.55,6.15,5.45,5.70,6.1250,5.67,5.75,5.55,5.35,5.55,5.9750,5.80,5.60,5.9125,5.9375,5.25,5.85,5.4375,6.10,5.55,,,5.61666666666666666667,5.61666666666666666667,5.25,6.25,6.1750,5.85,5.5375,5.8250,6.03,,,,6.10,,,
5.95,5.46666666666666666667,6.45,5.7375,,5.55,5.66666666666666666667,5.70,,5.7750,5.50,5.65,5.90,5.8750,5.9750,6,5.6750,,5.81666666666666666667,,5.50,5.4250,5.65,6,6.1250,5.25,5.90,5.6875,5.6250,,6.2750,6.15,5.7750,5.9250,5.20,5.57,5.92,,5.35,,5.45,5.40,,,,,,,
5.65,4.95,5.70,,5.91666666666666666667,5.3750,5.3750,5.45,5.61666666666666666667,6.0250,5.8750,5.55,5.85,6.28333333333333333333,5.9125,5.55,7.85,5.50,6,,5.53,5.35,5.8250,5.75,5.90,5.71666666666666666667,5.70,5.90,5.4250,5.4750,5.93333333333333333333,5.95,5.9750,5.45,5.53333333333333333333,6.21666666666666666667,5.20,5.10,,5.37,5.7250,5.51666666666666666667,,,,,,,
5.76666666666666666667,5.65,5.95,,5.95,5.10,5.60,5.70,5.80,5.5625,6.10,5.45,5.5250,,5.6375,6.20,,5.60,5.3750,,5.95,5.5250,5.35,5.70,5.4250,5.75,5.0750,5.60,5.78333333333333333333,5.50,5.4250,5.85,6.05,5.05,5.7750,,6.20,6.40,5.35,5.6250,5.65,5.75,,,,5.50,,4.80,
5.9250,5.15,6.0250,5.15,5.40,,6,,,5.8750,,6.10,,5.6750,5.8750,,6.15,6.30,5.80,5.96666666666666666667,5.95,5.90,5.50,5.81666666666666666667,5.60,6.0750,5.10,4.95,5.95,5.35,,5.70,6.05,5.7750,5.7250,5.35,5.8250,5.55,5.76666666666666666667,7.65,5.75,5.8250,,,5.40,,,,
6.25,5.90,5.7250,5.85,5.85,5.50,6.65,5.0750,6.0250,5.98333333333333333333,,5.38333333333333333333,5.7750,5.5250,5.55,6,5.6250,5.75833333333333333333,5.2750,5.8750,5.71666666666666666667,5.48333333333333333333,,,,,5.45,6,5.65,5.15,5.66666666666666666667,5,5.9750,5.5750,,5.10,5.51666666666666666667,5.8250,6.03333333333333333333,,,5.55,,6.20,5.40,,,5.55,
,,5.5750,,5.5250,6.20,5.7750,5.80,5.56666666666666666667,6.35,6.05,,5.8250,6.05,6.45,5.65,,5.7250,5.65,,6.0750,5.30,5.88333333333333333333,6.0250,6.15,5.45,,5.20,6.25,,,5.61666666666666666667,5.93333333333333333333,6.06666666666666666667,5.6125,5.70833333333333333333,6.45,5.90,5.85,5.7250,5.40,,,,,,,,
5.70,5.55,5.55,5.75,5.60,5.75,5.75,6.2750,5.85,5.80,5.90,6.40,,5.20,6.10,5.5750,5.25,,5.4750,5.45,6.1250,,,5.90,6.0750,5.8250,5.85,5.28333333333333333333,5.7250,5.86666666666666666667,5.50,5.7750,5.85,5.5625,5.90,5.10,5.85,6.2250,5.75,5.75,5.6750,5.20,,,,,,,
,5.73333333333333333333,5.75,,5.8250,5.75,5.40,5.60,6.18333333333333333333,5.70,5.95,,5.55,,5.58333333333333333333,6,,5.15,5.75,,5.70,5.81666666666666666667,5.80,6.45,5.2750,5.65,5.50,5.45,6.20,5.85,5.60,5.88333333333333333333,5.95,5.70,,,6.01666666666666666667,,,,5.15,5.8250,,,,,,,
6.30,5.85,,,5.5250,5.65,5.50,6.55,5.4750,,5.25,5.95,5.3750,6.15,5.68333333333333333333,5,5.35,5.59,5.4750,5.5750,5.75,,6.45,,5.65,5.7750,5.76666666666666666667,,5.65,5.7750,,5.55,5.85,5.85,5.65,5.2750,5.70,5.71666666666666666667,5.7750,6.70,5.61666666666666666667,,,,,,,,
5.60,5.9750,5.95,5.8750,5.55,6.20,5.30,5.50,6.20,,6.15,5.40,5.65,5.96666666666666666667,5.50,,5.60,6.25,5.25,6.35,,5.65,5.90,5.60,5.8750,,6,5.95,5.70,5.70,5.8750,,5.85,5.30,5.90,5.3750,6.0750,6.05,5.50,,5.25,6.20,,,,,,,
5.65,5.50,5.55,5.75,5.9750,5.33,6,5.7750,6.2250,5.50,5.20,5.30,6.0250,5.93333333333333333333,6.30,5.86666666666666666667,,5.30,5.95,6.0125,5.80,5.45,5.25,,,5.5375,5.85,,5.51666666666666666667,,5.85,5.50,5.50,5.25,5.70,5.70,5.80,,,5.45,5.9875,,,6.25,,,,,
6,,5.55,5.40,6.10,6.05,,5.3750,5.65,5.70,5.7750,6.05,5.50,5.61666666666666666667,5.43333333333333333333,5.6875,,5.6750,5.95,,,5.5250,5.6750,5.85,5.9250,5.80,5.95,5.60,5.80,5.66666666666666666667,,6,5.7250,5.50,5.65,6.05,6.01666666666666666667,6.05,6.20,6.10,,6.15,,,,,,,
,5.6875,6,5.55,,5.7250,5.55,5.60,5.65,5.56666666666666666667,5.99,5.10,5.9750,,5.9750,5.30,5.9250,9.70,5.60,6.40,6.15,6.1250,6.10,5.80,5.80,,5.60,,5.50,,5.95,5.7750,5.65,,,5.70,,5.98333333333333333333,,,5.95,5.66666666666666666667,,,,,,,
Your problem isn't the NA values, it's the fact that the contour functions want a matrix, and you're passing a data.frame. If you use as.matrix on it, it converts to character mode, presumably because your last column is entirely missing (so is read as mode logical, not numeric). Use this massaging on it and the plot will work: myData <- as.matrix(as.data.frame(lapply(myData,as.numeric))) Duncan Murdoch
"Duncan Murdoch" <murdoch at math.aau.dk> wrote in message news:425CFE4D.8010208 at math.aau.dk...
WeiQiang.Li at seagate.com wrote:
Your problem isn't the NA values, it's the fact that the contour functions want a matrix, and you're passing a data.frame. If you use as.matrix on it, it converts to character mode, presumably because your last column is entirely missing (so is read as mode logical, not numeric). Use this massaging on it and the plot will work: myData <- as.matrix(as.data.frame(lapply(myData,as.numeric)))
This looks unnecessarily complicated here, but appears to be necessary. I normally would try to use only "as.matrix" here but this can fail as shown below, but sometimes can work. R's "rules" about this conversion seem somewhat arbitrary to me. Example 3, in particular, doesn't make sense to me. Can anyone share some insight on what is going on? ========================== Dummy.txt 1,2,3 4,5,6 # 1. Integers, no missing values; "as.matrix" good enough for conversion # Results make sense.
myData <- read.table('Dummy.txt',sep=',')
typeof(myData)
[1] "list"
class(myData)
[1] "data.frame"
myData <- as.matrix(myData)
myData
V1 V2 V3 1 1 2 3 2 4 5 6
typeof(myData)
[1] "integer"
class(myData)
[1] "matrix" ========================== Dummy.txt 1,,3 4,2.5,6 # 2. Doubles, missing values; "as.matrix" good enough for conversion # Results make sense.
myData <- read.table('Dummy.txt',sep=',')
myData <- as.matrix(myData)
myData
V1 V2 V3 1 1 NA 3 2 4 2.5 6
typeof(myData)
[1] "double"
class(myData)
[1] "matrix" ========================== Dummy.txt 1,,3 # 3. Drop second row of data from 2 above. Now instead of integers or doubles, # the type is "character" after using as.matrix? # Results don't make sense. Why did dropping the second row of data change # the type to "character" here?
myData <- read.table('Dummy.txt',sep=',')
myData <- as.matrix(myData)
myData
V1 V2 V3 1 "1" NA "3"
typeof(myData)
[1] "character"
class(myData)
[1] "matrix" ========================== Dummy.txt 1,,3 # 4. More complicated solution than 3 above, like what Duncan suggested, # but this gives expected results
myData <- read.table('Dummy.txt',sep=',')
myData <- as.matrix(as.data.frame(lapply(myData,as.numeric)))
myData
V1 V2 V3 1 1 NA 3
typeof(myData)
[1] "double"
class(myData)
[1] "matrix" ========================== Thanks for any help in clarifying this R subtilty. efg -- Earl F. Glynn Scientific Programmer Stowers Institute for Medical Research