Skip to content

lines through points in lattice legend

4 messages · Martin Gregory, Deepayan Sarkar, Ken Knoblauch

#
Hi,

I'm struggling to find if there is a simple way to make the lines and points overlap in a legend for a lattice plot using auto.key. ?Here is a toy example of what doesn't work (for me) as the lines and points are adjacent rather than overlapping:

library(lattice)
d <- data.frame(x = 1:2, y = 1:4, f = factor(letters[1:2]))

xyplot(y ~ x, d, groups = f, type = "b",?
	pch = 21:22, fill = c("white", "black"), col = "black",
	par.settings = list(superpose.symbol =?
	list(pch = 21:22, fill = c("white", "black"), col = "black"),
	superpose.line = list(col = "black")),
	auto.key = list(space = "right", points = TRUE, lines = TRUE))

I've seen a few examples on stack.overflow but I haven't been able to make them work, or they seem more complicated then I would think it should be (but I don't exclude that I'm fooling myself there).

R version 4.2.2 Patched (2023-01-04 r83564)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-45

loaded via a namespace (and not attached):
[1] compiler_4.2.2 grid_4.2.2    
     
 Thanks for any enlightenment, in advance.

Ken 
 
  ___
  Kenneth Knoblauch
 Inserm U1208
 Stem-cell and Brain Research Institute
 18 avenue du Doyen L?pine
 69500 Bron
 France
 tel: +33 (0)4 72 91 34 77
 fax: +33 (0)4 72 91 34 61
 portable: +33 (0)6 84 10 64 10
 https://sbri.fr/public-profile/63/single-member/
#
Hi Kenneth,

replacing the auto.key argument with

       key = list(space = "right",
                  lines = list(type="b", pch=21:22, fill = c("white", "black")),
                  text = list(c("a","b")))

works, but is definitely not simpler. Given that your choice of points is so distinctive, why not just use the points in the legend?

Regards,
Martin
#
On Sat, Jan 28, 2023 at 2:49 PM Kenneth Knoblauch
<ken.knoblauch at inserm.fr> wrote:
Just adding a type = "b" should work (for the lines), so something like:

       auto.key = list(space = "right", type = "b",
                       points = FALSE, lines = TRUE)

BTW, once you specify par.settings, you don't need to specify the
parameters again, so you can drop the second line.

Best,
-Deepayan
#
Thanks. ?That works well and it's simple.  

Ken

  ___
  Kenneth Knoblauch
 Inserm U1208
 Stem-cell and Brain Research Institute
 18 avenue du Doyen L?pine
 69500 Bron
 France
 tel: +33 (0)4 72 91 34 77
 fax: +33 (0)4 72 91 34 61
 portable: +33 (0)6 84 10 64 10
 https://sbri.fr/public-profile/63/single-member/
    




De : Deepayan Sarkar <deepayan.sarkar at gmail.com>
Envoy? : samedi 28 janvier 2023 16:46
? : Kenneth Knoblauch
Cc?: r-help at r-project.org
Objet : Re: [R] lines through points in lattice legend
?   
On Sat, Jan 28, 2023 at 2:49 PM Kenneth Knoblauch
<ken.knoblauch at inserm.fr> wrote:
Just adding a type = "b" should work (for the lines), so something like:

?????? auto.key = list(space = "right", type = "b",
?????????????????????? points = FALSE, lines = TRUE)

BTW, once you specify par.settings, you don't need to specify the
parameters again, so you can drop the second line.

Best,
-Deepayan