简体   繁体   中英

How can you create Marginal Histogram Scatterplot using lattice package (not ggplot2)?

Long story short, I am working on an assignment for a data visualization course and the assignment specifies that we have to use the lattice package and that we have to create a marginal histogram scatterplot. (I know that asking about homework questions is frowned upon, but I'm not asking you to write my assignment for me - only asking for guidance or at least a direction to start in).

Our lecture and book don't mention anything about marginal histogram scatterplots and while the lecture shows how to create them using the standard plot function in R as well as how to do it using ggplot2, we are told not to use either. I've never used lattice before, and when I ask for help, I get general responses that aren't helpful at all.

Note: I'm not posting the question or what type of data I have to use as I'm not looking for an answer to the homework here. Just some help on where to begin. You can literally use any data if you want to show an example.

This is definitely a tricky question in lattice as well. There are quite some compelling reasons why ggplot2 has become one of the more popular packages, while lattice is still extremely powerful. As this is part of a visualization course, I'd assume you are meant to come up with something similar to ggMarginal . For this you'll have to use some time adjusting margin s on your lattice plot.

As a guideline for how I'd solve this question, I found an answer doing the following:

  1. Search google for lattice marginal histogram , the second link is an answer to a mailing help list, which gives an example to a similar problem
  2. Open R and following the link make a small example. Eg.
data(mtcars)
library(lattice)
scatter <- xyplot(hp ~ mpg, mtcars)
hist <- histogram(~ mpg, mtcars)
plot(scatter, more = TRUE, split = c(1, 2, 1, 2))
plot(hist, more = FALSE, split = c(1, 1, 1, 2))

裂图点阵

  1. after getting this far, it comes about figuring what is actually happening. The link above suggests looking at ?plot.trellis , and the importance here seems how can we move around our plots, which seems to be controlled by split . Looking at the documentation ( ?plot.trellis ) we get some help for understanding how to use this argument

a vector of 4 integers, c(x, y, nx, ny) , that says to position the current plot at the x , y position in a regular array of nx by ny plots. (Note: this has origin at top left)

From here we have everything we need to create the marginal plot, If we make this a 2x2 plot, we'd place one histogram at c(1, 1, 2, 2) , a scatter plot at c(2, 1, 2, 2) and another histogram at c(2, 2, 2, 2) . Of course this is not going to be the best looking marginal plot, for which you'd have to work with the margin s or go under the hood and manually set up the plot using the grid package. I'd say that is definitely a bit on the "next level" side of thing.

Note:

In the above example I didn't cover how one can rotate one histogram, or how one can create a sideways histogram, if you are seeking to replicate ggMarginal more closely.
In addition as you said you had some problems finding information on this. Another option for finding an answer would've been reading the ?histogram documentation page. There are several examples within this page (and many others) which show how one can manipulate the position of lattice plots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM