How do I specify different color ranges for different levels?

后端 未结 1 490
青春惊慌失措
青春惊慌失措 2021-02-08 12:39

I am making a lattice levelplot from x and y factors that range from [0,1]:

      x     y     level                                


        
相关标签:
1条回答
  • 2021-02-08 13:10

    This is revision #3

    Here we go (again). :)

    This is weird, if I set n to anything below 15, things seem to work?

    enter image description here

    df <- read.table("http://dl.dropbox.com/u/31495717/stackoverflow.overlaps.list.txt",
            sep = "\t", header = FALSE)
    names(df) <- c("x", "y", "level")
    df$level <- round(df$level*100, 0)
    
    n <- 10
    col.seq <- seq(10, 80, length.out = n)
    brks <- c(0, seq(10, 80, length.out = n), 100)
    cuts <- cut(df$level, breaks = brks)
    colors <- colorRampPalette(c("red", "white"))(length(levels(cuts))-1)
    colors <- c(colors, "black")
    
    cls <- rep(colors, times = table(cuts))
    
    print(levelplot(cuts~x*y,
                    data = df,
                    cuts = n,
                    col.regions=cls,
                    xlab="",
                    ylab="",
                    aspect="iso",
                    scales=list(
                            x=list(rot=90)
                    ),
                    panel=function(...) {
                        arg <- list(...)
                        panel.levelplot(...)
                        panel.text(df$x, df$y, df$level, cex=0.5)
                    },
                    colorkey = list(col = colors, at = brks)
            ))
    
    0 讨论(0)
提交回复
热议问题