Contour plot
# acceleration for each combination of wt and hp # Feed the dataframe into the loess model and receive a matrix output with estimates of # Generate a dataframe with every possible combination of wt and hpĭata.fit <- id ( wt = xgrid, hp = ygrid)
Ygrid <- seq ( min (mtcars$hp), max (mtcars$hp), 0.3 ) Xgrid <- seq ( min (mtcars$wt), max (mtcars$wt), 0.3 ) # Create a sequence of incrementally increasing (by 0.3 units) values for both wt and hp These estimates are stored in a matrix where each element of the wt sequence is represented by a row and each element of the hp sequence is represented by a column. Using the predict function, the loess model object is used to estimate a qsec value for each combination of values in the two sequences. An arbitrarily chosen distance of 0.3 between sequence elements was used to give a relatively fine resolution to the data. We do that by generating a sequence of values with uniform spacing over the range of wt and hp. The model contained within the resulting loess object is then used to output the three-dimensional dataset needed for plotting. To compute the estimates, a two-dimensional loess model is fit to the data using the following call:ĭata.loess <- loess (qsec ~ wt * hp, data = mtcars) We are interested in estimating acceleration for all possible combinations of weight and horsepower using the available data, thereby generating three dimensional data.
The contour function requires three dimensional data as an input. Preparing the Data for Contour Plots in Base R To get a different representation of these relations, we use contour plots. This plot provides a first look at the interrelationships of the three variable of interest. Panel.background = element_rect ( fill = “black” )) + Legend.background = element_rect ( colour = “black” ),
Ggplot (mtcars, aes ( x = wt, y = hp, color = factor (quart))) + Labels were also manually added to improve interpretation. We selected colors that were sequential and color blind friendly using ColorBrewer and manually added them to the scale_colour_manual() argument within the ggplot() call below. Mtcars$quart <- cut (mtcars$qsec, quantile (mtcars$qsec))įrom here, we use ggplot to visualize the data. We first create a new variable to represent quartiles of acceleration using the cut and quantile functions. To start, we look at a simple scatter plot of weight by horsepower, with each data point colored according to quartiles of acceleration. # mpg cyl disp hp drat wt qsec vs am gear carb It is reasonable to believe that weight and horsepower are jointly related to acceleration, possibly in a nonlinear fashion. qsec is a measure of acceleration with shorter times representing faster acceleration. We focus on the following three variables: wt (weight, 1000lbs), hp (gross horsepower), qsec (time required to travel a quarter mile). The mtcars dataset provided with Base R contains results from Motor Trend road tests of 32 cars that took place between 19. We also describe several data transformations needed to accomplish this visual exploration. We then develop visualizations using ggplot2 to gain more control over the graphical output. We use the contour function in Base R to produce contour plots that are well-suited for initial investigations into three dimensional data. To visually explore relations between two related variables and an outcome using contour plots.