Reputation: 57
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.
Upvotes: 0
Views: 471
Reputation: 8582
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:
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))
?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 argumenta vector of 4 integers,
c(x, y, nx, ny)
, that says to position the current plot at thex
,y
position in a regular array ofnx
byny
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.
Upvotes: 1