[R] How to plot Contour with NA in dataframe
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.6667,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.7833,,5.55, ,5.5250,4.856.15,5.8667,,5.20,5.5750,5.74,5.7667,6.7333,6.05,5.8833,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.5667,5.9750,6.0250,,,6.35 5.45,,6.30,5.7167,,5.50,6.05,5.8333,5.5833,5.6250,5.70,5.9250,5.7750,6.20,5.5333,5.6625,5.40,5.75,,6,5.5333,,5.6250,5.55,5.65,5.55,5.70,,6.15,5.90,5.7167,6.1167,5.15,6.10,5.4750,,5.75,5.45,5.95,,5.95,5.36676.15,,, ,5.7250,5.75,6,,,5.65,,6.30,5.65,,5.4750,5.60,5.4167,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.5667,5.75,,,6,5.85,5.75,5.7250,5.75 5.75,,6.1167,,5.3250,,5.9167,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.9667,5.6833,5.90,5.8333,5.6750,,5.6750,5.7250,5.6833,5.6333,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.5833,5.8333,5.9250,5.7750,5.6667,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.7667,,,5.60,, ,,,5.15,,6.05,5.4333,5.70,,5.75,6.05,5.15,5.75,5.60,,6.1750,,6.055.5625,5.4167,,5.6250,5.50,5.90,5.95,5.90,6.0250,5.9667,6,5.70,6,5.90,6,5.55,5.8167,5.7750,5.30,,5.77505.40,,, ,6.0667,5.45,5.60,5.55,5.95,5.8750,5.8750,5.80,5.91675.85,5.75,5.5167,,5.8750,,5.45,6.0250,,5.80,5.9375,5.7750,5.5167,,5.8750,5.60,5.8625,5.6333,,5.50,5.2625,5.65,,5.30,5.35,,5.7250,,5.8333,5.85,5.70,,5.30,,, 5.7750,6,5.55,,5.45,5.4125,5.9917,5.50,5.65,5.9750,,,5.7667,5.8333,5.79,5.85,,5.90,5.9833,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.8833,5.80,5.95,5.60,5.75,,5.90,6.45,,, 5.6833,5.60,,5.55,5.1250,,6.05,5.75,5.75,5.6833,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.6167,5.6167,5.25,6.25,6.1750,5.85,5.5375,5.8250,6.036.10,,, 5.95,5.4667,6.45,5.7375,,5.55,5.6667,5.70,,5.7750,5.50,5.65,5.90,5.8750,5.9750,6,5.6750,,5.8167,,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.9167,5.3750,5.3750,5.45,5.6167,6.0250,5.8750,5.55,5.85,6.2833,5.9125,5.55,7.85,5.50,6,,5.53,5.35,5.8250,5.75,5.90,5.7167,5.70,5.90,5.4250,5.4750,5.9333,5.95,5.9750,5.45,5.5333,6.2167,5.20,5.10,,5.37,5.7250,5.5167,,, 5.7667,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.7833,5.50,5.4250,5.85,6.05,5.05,5.7750,,6.20,6.40,5.35,5.6250,5.65,5.755.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.9667,5.95,5.90,5.50,5.8167,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.7667,7.65,5.75,5.8250,,,5.40
Re: [R] How to plot Contour with NA in dataframe
[EMAIL PROTECTED] 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.6667,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.7833,,5.55, ,5.5250,4.856.15,5.8667,,5.20,5.5750,5.74,5.7667,6.7333,6.05,5.8833,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.5667,5.9750,6.0250,,,6.35 5.45,,6.30,5.7167,,5.50,6.05,5.8333,5.5833,5.6250,5.70,5.9250,5.7750,6.20,5.5333,5.6625,5.40,5.75,,6,5.5333,,5.6250,5.55,5.65,5.55,5.70,,6.15,5.90,5.7167,6.1167,5.15,6.10,5.4750,,5.75,5.45,5.95,,5.95,5.36676.15,,, ,5.7250,5.75,6,,,5.65,,6.30,5.65,,5.4750,5.60,5.4167,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.5667,5.75,,,6,5.85,5.75,5.7250,5.75 5.75,,6.1167,,5.3250,,5.9167,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.9667,5.6833,5.90,5.8333,5.6750,,5.6750,5.7250,5.6833,5.6333,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.5833,5.8333,5.9250,5.7750,5.6667,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.7667,,,5.60,, ,,,5.15,,6.05,5.4333,5.70,,5.75,6.05,5.15,5.75,5.60,,6.1750,,6.055.5625,5.4167,,5.6250,5.50,5.90,5.95,5.90,6.0250,5.9667,6,5.70,6,5.90,6,5.55,5.8167,5.7750,5.30,,5.77505.40,,, ,6.0667,5.45,5.60,5.55,5.95,5.8750,5.8750,5.80,5.91675.85,5.75,5.5167,,5.8750,,5.45,6.0250,,5.80,5.9375,5.7750,5.5167,,5.8750,5.60,5.8625,5.6333,,5.50,5.2625,5.65,,5.30,5.35,,5.7250,,5.8333,5.85,5.70,,5.30,,, 5.7750,6,5.55,,5.45,5.4125,5.9917,5.50,5.65,5.9750,,,5.7667,5.8333,5.79,5.85,,5.90,5.9833,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.8833,5.80,5.95,5.60,5.75,,5.90,6.45,,, 5.6833,5.60,,5.55,5.1250,,6.05,5.75,5.75,5.6833,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.6167,5.6167,5.25,6.25,6.1750,5.85,5.5375,5.8250,6.036.10,,, 5.95,5.4667,6.45,5.7375,,5.55,5.6667,5.70,,5.7750,5.50,5.65,5.90,5.8750,5.9750,6,5.6750,,5.8167,,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.9167,5.3750,5.3750,5.45,5.6167,6.0250,5.8750,5.55,5.85,6.2833,5.9125,5.55,7.85,5.50,6,,5.53,5.35,5.8250,5.75,5.90,5.7167,5.70,5.90,5.4250,5.4750,5.9333,5.95,5.9750,5.45,5.5333,6.2167,5.20,5.10,,5.37,5.7250,5.5167,,, 5.7667,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.7833,5.50,5.4250,5.85,6.05,5.05,5.7750,,6.20,6.40,5.35,5.6250,5.65,5.755.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.9667,5.95,5.90,5.50,5.8167,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.7667,7.65,5.75,5.8250,,,5.40
Re: [R] How to plot Contour with NA in dataframe
Duncan Murdoch [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [EMAIL PROTECTED] 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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html