Visualizing 3d data – plotting quartiles separately

Visualizing 3d data – plotting quartiles separately

In this previous post, we’ve looked at displaying three dimensional data.  One major problem is when there is a high density of data, it can be difficult to see what’s going on in a 3 dimensional plot.

One way of looking at the data in more detail is to break it up.  Take a look at this graph:

This is a plot of data of air quality in Nottingham, UK, taken hourly in 2009 (the code to create it in base R is on the bottom of the page).  On the left is a scatterplot of NO2 against ozone (plot A).   The different colours indicate grouping the data by the level of ozone into quartiles.  On the right are plots of the NO vs NO2 for the same data, but a  separate plot for each quartile of the ozone data.  The points are all colour co-ordinated, so the red points indicating the upper quartile of the ozone data in plot A are matched by red points in plot B.

So you can see by comparing plot E and D, that at the lowest quartile of ozone levels, there is a greater spread of both NO2 and NO.

How this is done is pretty simple (most of the code is to make things vaguely pretty).  Essentially, the values for x,y and z are put into a matrix xyz.  The rows of the matrix are ordered according to the z variable.  The rows which deliniate each quartile are calculated, and then the plots for B to E of x vs y are drawn, using only the rows for that quartile.  The axes are plotted so that they are the same scale for each of the plots. There’s not much room for the axis labels – so these are added afterwards with the legend command.

Then on the left the plot for y (on the horizontal axis) and z (on the vertical axis) is drawn, with some added lines to show where the boundaries of each quartile lie.  The colours are stored in the xyz matrix in the col column.  Like most of my code, the graph is portable, you just need to input different values for x, y and z and re-label the names for each variable.  The original dataset is the same one which I have used for my previous posts.  It is from the UK airquality database.  If you copy this file into your working directory and run the code below, you’ll repeat the plot.

Any suggestions for improvements / comments would be most appreciated!

01 ## name the columns of the data
02 columns <- c("date", "time", "NO", "NO_status", "NO_unit", "NO2",
03     "NO2_status", "NO2_unit", "ozone", "ozone_status", "ozone_unit",
04     "SO2", "SO2_status", "SO2_unit")
05 ## read in the data, store it in variable data
06 data <- read.csv("27899712853.csv", header = FALSE, skip = 7,
07     col.names = columns, stringsAsFactors = FALSE)
08  
09 ## now make the x,y and z variables
10  
11 x <- data$NO
12 y <- data$NO2
13 z <- data$ozone
14 cols <- rep(1,length(z))
15  
16 xyz <- cbind (x,y)
17 xyz <- cbind(xyz,z)
18 xyz <- cbind(xyz,cols)
19 colq1 <- 6
20 colq2 <- 4
21 colq3 <- 3
22 colq4 <- 2
23  
24 xl <- "NO"
25 yl <- "NO2"
26 zl <- "Ozone"
27  
28 point <- 20
29  
30 # re order by z
31 xyz <- xyz[order(xyz[,3]),]
32 # now define the row numbers for the quartile boundries
33 maxxyz <-  nrow(xyz)
34 q1xyz <- round(maxxyz/4)
35 medianxyz <-  round(maxxyz/2)
36 q3xyz <- round(maxxyz*3/4)
37  
38 # assign colours to xyz$col
39 xyz[1:q1xyz,4] <- colq1
40 xyz[q1xyz:medianxyz,4] <- colq2
41 xyz[medianxyz:q3xyz,4] <- colq3
42 xyz[q3xyz:nrow(xyz),4] <- colq4
43  
44 # define the maximum values for x,y, and z
45 # these are used to ensure all the axes are the same scale
46 maxx <- x[which.max(x)]
47 maxy <- y[which.max(y)]
48 maxz <- z[which.max(z)]
49  
50 # now make the plot
51 # first job is to save the graphics parameters currently used
52 def.par <- par(no.readonly = TRUE)
53 # define the margins around each plot
54 par("mar" = c(2,2,0.5,0.5))
55 # make the layout for the plot
56 layout(matrix(c(5,1,5,2,5,3,5,4), 4, 2, byrow = TRUE))
57  
58 # now do the four plots on the right
59 plot(xyz[q3xyz:maxxyz,1],xyz[q3xyz:maxxyz,2], col = colq4,
60     xlab = xl, ylab = yl, pch=point, xlim = c(0,maxx),
61     ylim = c(0,maxy))
62 legend(x = "right", yl, bty = "n")
63 legend(x = "topright", "B", bty = "n")
64  
65 plot(xyz[medianxyz:q3xyz,1],xyz[medianxyz:q3xyz,2], col = colq3,
66     pch=point, xlim = c(0,maxx), ylim = c(0,maxy))
67 legend(x = "right", yl, bty = "n")
68 legend(x = "topright", "C", bty = "n")
69  
70 plot(xyz[q1xyz:medianxyz,1],xyz[q1xyz:medianxyz,2], col = colq2,
71     pch=point, xlim = c(0,maxx), ylim = c(0,maxy))
72 legend(x = "right", yl, bty = "n")
73 legend(x = "topright", "D", bty= "n")
74  
75 plot(xyz[0:q1xyz,1],xyz[0:q1xyz,2], col = colq1, pch=point,
76     xlim = c(0,maxx), ylim = c(0,maxy))
77 legend(x = "right", yl, bty = "n")
78 legend(x = "bottom", xl, bty = "n")
79 legend(x = "topright", "E", bty = "n")
80  
81 # now do the plot on the left
82 plot(xyz[,2],xyz[,3], col = xyz[,4], pch=point, xlim = c(0,maxy))
83 legend(x = "bottom", yl, bty = "n")
84 legend(x = "right", zl, bty = "n")
85 legend(x = "topright", "A", bty = "n")
86  
87 abline(h=xyz[q1xyz,3],col=3,lty=2)
88 abline(h=xyz[medianxyz,3],col=4)
89 abline(h=xyz[q3xyz,3],col=5,lty=2)
90  
91 ## reset the graphics display to default
92 par(def.par)

Tags: