简体   繁体   中英

Points on lines in lattice plot legend in R

How to draw a legend using lattice where points are shown in the middle of the lines?

There are two great and relevant answers: Include lines and points in lattice legend plot in R and http://r.789695.n4.nabble.com/How-to-overlay-lines-and-rectangles-in-lattice-plot-key-td4727682.html

Unfortunately, when I change the space parameter to anything else than right :

auto.key=list(space="right",lines=TRUE,points=TRUE) # worked fine

auto.key=list(space="left",lines=TRUE,points=TRUE) # error message
auto.key=list(x=0, y=0,lines=TRUE,points=TRUE) # error message

I get the following error:

Error in do.call("fun", legend[[i]]$args, quote = TRUE) : 
  second argument must be a list

How can I fix it?

Apologies if the solution is obvious. I am new to R and lattice but have searched for couple of days. Many thanks for help.

Example:

drawComboKey <- function(...) {
    key = simpleKey(...)
    key = draw.key(key, draw = FALSE)

    ngroups <- (length(key$children)-1)/3
    #remove points column
    key$framevp$layout$ncol        <- key$framevp$layout$ncol-3L
    key$framevp$layout$respect.mat <- key$framevp$layout$respect.mat[,-(3:5)]
    key$framevp$layout$widths      <- key$framevp$layout$widths[-(3:5)]

    #adjust background
    key$children[[1]]$col[2]                   <- key$children[[1]]$col[2]-3L
    key$children[[1]]$cellvp$layout.pos.col[2] <- key$children[[1]]$cellvp$layout.pos.col[2]-3L
    key$children[[1]]$cellvp$valid.pos.col[2]  <- key$children[[1]]$cellvp$valid.pos.col[2]-3L

    #combine lines/points
    mylines<-(2+ngroups*2):(1+ngroups*3)
    for(i in mylines) {
        key$children[[i]]$children <- gList(key$children[[i-ngroups]]$children, key$children[[i]]$children)
        key$children[[i]]$childrenOrder         <- names(key$children[[i]]$children)
        key$children[[i]]$col                   <- key$children[[i]]$col-3L
        key$children[[i]]$cellvp$layout.pos.col <- key$children[[i]]$cellvp$layout.pos.col-3L
        key$children[[i]]$cellvp$valid.pos.col  <- key$children[[i]]$cellvp$valid.pos.col-3L
    }

    key$childrenOrder<-names(key$children)
    key
}

library(grid)
library(lattice)
library(latticeExtra)
my.chart <- xyplot(
    Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width, 
    iris,
    type = c("p", "l"),
    auto.key = list(space="right", lines=TRUE, points=TRUE) # works fine
    #auto.key = list(space="left", lines=TRUE, points=TRUE) # error message

    #  why is this not working?
    #legend = list(right = list(fun = drawComboKey))
    #  forced below by: my.chart$legend$right$fun = "drawComboKey"
)
my.chart$legend$right$fun = "drawComboKey"
plot(my.chart)

You're almost there... Be aware that for space = "left" , you need to apply your custom function drawComboKey() to the legend on the left . This analogously applies to space = "bottom" (or "top" ).

my.chart = xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width, 
                  data = iris, type = "b",
                  auto.key = list(space="left", lines = TRUE, points = TRUE))

my.chart$legend$left$fun = "drawComboKey" # same position as 'space' above
plot(my.chart)

xyplot(1)

Note that when working with x, y instead of space , the function needs to be passed to my.chart$legend$inside$fun .

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