The process of overlaying plots in a trellis graph involves creating multiple plots on the same graph, each representing a different subset of the data. These plots are arranged in a grid, with each plot sharing the same axes and scale. This allows for easy comparison and identification of patterns and trends across the different subsets of data. The trellis graph is a useful tool for visualizing and analyzing data with multiple variables, as it allows for efficient and comprehensive exploration of the data.
How do I overlay plots in a trellis graph? | R FAQ
You can download the https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsb2-1.csv data frame
that will be used in these examples.
Suppose that we would like to overlay two different plots in one
single trellis graph. For example, we would like to overlay two plots
consisting of a scatter plot with a regression line, but in each graph we would
like to have a different dependent and independent variable. In one trellis graph we
would like to have a scatter plot and regression line of read regressed on
write, and a scatter plot and regression line of science regression on math
both conditional on the variable ses.
Reading in the hsb2 data frame.
hsb2 <- read.table('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsb2-1.csv', header=T, sep=",")Also, you will need to load the package lattice before you start.
You can download lattice from the CRAN website from within R by clicking on
“Packages” and then “Install package(s) from CRAN”.
library(lattice)
Let’s first look at how we would generate each trellis graph individually.
#creating a factor variable ses.f from the variable ses hsb2$ses.f
xyplot(math~science | ses.f, hsb2,
panel=function(x, y){
panel.xyplot(x, y, pch=3)
panel.lmline(x, y)
}, as.table=T)In order to combine the two graphs we will make use of the subscript parameter,
which is a parameter in every panel function. The subscript parameter is an indicator
of which indices corresponds to which panel in the trellis graph. Thus, we include the
subscript parameter in the function we create for the panel argument in the
trellis graph. Since we want to have two scatter plots overlaid, we must have two panel.xyplot
functions; likewise, we want to have two regression lines in each plot, and therefore we must include
two panel.lmline functions. One of the panel.xyplot function will use the variables
specified in the formula argument (write~read | ses.f) of the
xyplot function which indicates which observation will be used in each of the panels.
In the other panel.xyplot function we need to use the subscript parameter to indicate
which observations will be used in each of the panels because we are no longer using the
variables in the formula given in the formula argument in the xyplot function.
xyplot(write~read | ses.f, hsb2,
panel=function(x, y, subscripts){
panel.xyplot(x, y, pch=16)
panel.lmline(x, y, lty=4)
panel.xyplot(hsb2$science[subscripts], hsb2$math[subscripts], pch=3)
panel.lmline(hsb2$science[subscripts], hsb2$math[subscripts])
}, as.table=T, subscripts=T)Cite this article
stats writer (2024). How do I overlay plots in a trellis graph?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-overlay-plots-in-a-trellis-graph/
stats writer. "How do I overlay plots in a trellis graph?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-overlay-plots-in-a-trellis-graph/.
stats writer. "How do I overlay plots in a trellis graph?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-overlay-plots-in-a-trellis-graph/.
stats writer (2024) 'How do I overlay plots in a trellis graph?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-overlay-plots-in-a-trellis-graph/.
[1] stats writer, "How do I overlay plots in a trellis graph?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do I overlay plots in a trellis graph?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.



