如何使用levelplot更改xticks位置和自定义图例(点阵库)

时间:2013-11-07 22:06:37

标签: r lattice

  1. 我试图将x-ticks和x-labels的位置从图的底部移动到顶部。

  2. 此外,我的数据有一堆NAs。目前,levelplot只是移除它们并将它们留在图中的空白处。我想知道是否可以将此NAs添加到图例中。

  3. 有什么建议吗?谢谢!

    这是我的代码及其输出:

    require(lattice)
    # see data from dput() below
    rownames(data)=data[,1]
    data_matrix=as.matrix(data[,2:11])
    color = colorRampPalette(rev(c("#D73027", "#FC8D59", "#FEE090", "#FFFFBF", "#E0F3F8", "#91BFDB", "#4575B4")))(100)
    levelplot(data_matrix, scale=list(x=list(rot=45)), ylab="Days", xlab="Strains", col.regions = color)
    

    数据

    data <- 
    structure(list(X = structure(1:17, .Label = c("Arcobacter", "Bacillus", 
    "Bordetella", "Campylobacter", "Chlamydia", "Clostridium ", "Corynebacterium", 
    "Enterococcus", "Escherichia", "Francisella", "Legionella", "Mycobacterium", 
    "Pseudomonas", "Rickettsia", "Staphylococcus", "Streptococcus", 
    "Treponema"), class = "factor"), day.0 = c(NA, -3.823301154, 
    NA, NA, NA, -3.518606107, NA, NA, NA, NA, NA, -4.859479387, NA, 
    NA, NA, -2.588402346, -2.668136603), day.2 = c(-4.006281239, 
    -3.024823788, NA, -5.202804501, NA, -3.237622321, NA, NA, -5.296138823, 
    -5.105469059, NA, NA, -4.901775198, NA, NA, -2.979144202, -3.050083791
    ), day.4 = c(-2.880770182, -3.210165554, -4.749097175, -5.209064234, 
    NA, -2.946480184, NA, -5.264113795, -5.341881713, -4.435780293, 
    NA, -4.810650076, -4.152531609, NA, NA, -3.106172794, -3.543161966
    ), day.6 = c(-2.869833226, -3.293283924, -3.831346387, NA, NA, 
    -3.323947791, NA, NA, NA, NA, NA, -4.397581863, -4.068855504, 
    NA, NA, -3.27028378, -3.662618619), day.8 = c(-3.873589331, -3.446192193, 
    -3.616207965, NA, NA, -3.13869325, NA, -5.010807453, NA, NA, 
    NA, -4.091502649, -4.412399025, -4.681675749, NA, -3.404738625, 
    -3.955464159), day.15 = c(-5.176583159, -2.512963066, -3.392832457, 
    NA, NA, -3.194662968, NA, -3.60440455, NA, NA, -4.875554468, 
    -2.507376205, -4.727255906, -5.27116754, -3.200499549, -3.361296145, 
    -4.320554841), day.22 = c(-4.550052847, -3.654013004, -3.486879661, 
    NA, NA, -3.614890858, NA, NA, NA, NA, -4.706690492, -2.200533317, 
    -4.836957953, NA, -4.390423731, NA, NA), day.29 = c(-4.730006329, 
    -3.46707372, -3.594457287, NA, NA, -3.800757834, NA, NA, NA, 
    NA, -4.285154089, -2.121152491, -4.816807055, -5.064577888, -2.945243736, 
    -4.479177287, -5.226435146), day.43 = c(-4.398680025, -3.144603215, 
    -3.642065153, NA, NA, -3.8268662, NA, NA, NA, NA, -4.762539208, 
    -2.156862316, -4.118608495, NA, -4.030291084, -4.678213147, NA
    ), day.57 = c(-4.689982547, -2.713502214, -3.51279797, NA, -5.069579266, 
    -3.495580794, NA, NA, NA, NA, -4.515973639, -1.90591075, -4.134826117, 
    -4.479351427, -3.482134037, -4.538534489, NA)), .Names = c("X", 
    "day.0", "day.2", "day.4", "day.6", "day.8", "day.15", "day.22", 
    "day.29", "day.43", "day.57"), class = "data.frame", row.names = c("Arcobacter", 
    "Bacillus", "Bordetella", "Campylobacter", "Chlamydia", "Clostridium ", 
    "Corynebacterium", "Enterococcus", "Escherichia", "Francisella", 
    "Legionella", "Mycobacterium", "Pseudomonas", "Rickettsia", "Staphylococcus", 
    "Streptococcus", "Treponema"))
    

    enter image description here

1 个答案:

答案 0 :(得分:4)

将标签移到顶部的请求非常简单(在查看scale部分下的?xyplot之后):

 levelplot(data_matrix, scale=list(x=list(rot=45,alternating=2)), 
           ylab="Days", xlab="Strains", col.regions = color)

尝试将NA值添加到颜色图例中可能需要更多思考,但似乎atcol的colorkey参数的合理值可能就足够了。

levelplot(data_matrix, scale=list(x=list(rot=45,alternating=2)), 
         ylab="Days", xlab="Strains", col.regions = color, 
         colorkey=list(at=as.numeric( factor( c( seq(-5.5, -2, by=0.5), 
                               "NA"))), 
                         labels=as.character( c( seq(-5.5, -2, by=0.5), 
                                     "NA")), 
                         col=c(color,  "#FFFFFF") ) )

enter image description here