R中条形图中数据值标签的顺序错误

时间:2012-02-10 01:35:23

标签: r bar-chart lattice

我正在尝试在使用网格包创建的条形图栏上添加数据值。下面是我的代码的快照。

不幸的是,条形图顶部的值不正确。例如,在条形图顶部,其值为3.02E-10,显示为4.76E-10,依此类推。有人可以帮我解决这个问题。

enter code here

p <- barchart (one_cir$Value~one_cir$bias, horiz=FALSE,xlim=select_bias, ylim=yrange, data=one_cir,
        groups=droplevels(one_cir$Model),
        xlab=list("RVBS (V)",cex=1.3 ),ylab=list("Delay (ps)",cex=1.3 ), 
        main="SVT Circuit Analysis: Delay vs RVBS",
        panel = function(x,y,...){
        panel.barchart(x,y,...)
        panel.grid(h=-1, v=0, col="gray")
        panel.text(x,y,label = one_cir$Value, pos=1,cex=1.2)
                },scales=list(cex=1.2),
        auto.key=list(x = .81, y = 1, corner = c(0, 0),half=FALSE,points = FALSE, cex=1.2, rectangles = TRUE)
                 )
        print(p)

样本数据集

bias Circuits Model Temp Corner Parameter    Value

0.0     NOR2   11C   25   3-TT delay_DDC 3.02e-10

0.3     NOR2   11C   25   3-TT delay_DDC 3.79e-10

0.6     NOR2   11C   25   3-TT delay_DDC 4.92e-10

0.0     NOR2   11B   25   3-TT delay_DDC 2.90e-10

0.3     NOR2   11B   25   3-TT delay_DDC 3.66e-10

0.6     NOR2   11B   25   3-TT delay_DDC 4.76e-10

1 个答案:

答案 0 :(得分:2)

我没有格子专业版,但是对panel.text()函数的以下调整正确地对标签进行了排序,并将标签放在各自的条形图上方,而不是与条形图重叠:

    panel.text(x + rep(c(.25, -.25), 3), y, label = one_cir$Value[order(one_cir$bias)], pos = 3, cex = 1.2)

这是我得到的输出:

lattice barchart with labels

我不知道是否有更多的方法来获得相同的结果。我也遗漏了xlim和ylim args,因为我没有那些物品。这是完整的代码:

    one_cir <- data.frame(bias = c(0.0, 0.3, 0.6, 0.0, 0.3, 0.6), 
    Circuits = rep("NOR2", 6), 
    Model = rep(c("11C", "11B"), each = 3), 
    Temp = rep(25,6), 
    Corner = rep("3-TT", 6), 
    Parameter = rep("delay_DDC", 6), 
    Value = c(3.02e-10, 3.79e-10, 4.92e-10, 2.90e-10, 3.66e-10,4.76e-10)
    )

    library(lattice)
    p <- lattice:::barchart(Value~bias, horiz = FALSE, data = one_cir,
            groups = droplevels(Model),
            xlab = list("RVBS (V)", cex = 1.3 ), ylab = list("Delay (ps)",cex = 1.3 ), 
            main = "SVT Circuit Analysis: Delay vs RVBS",
            panel = function(x,y,...){
                panel.barchart(x, y, ...)
                panel.grid(h = -1, v = 0, col = "gray")
                panel.text(x + rep(c(.25, -.25), 3), y, label = one_cir$Value[order(one_cir$bias)], pos = 3,cex = 1.2)
            },
            scales = list(cex = 1.2),
            auto.key = list(x = .81, y = 1, corner = c(0, 0), half = FALSE, points = FALSE, cex = 1.2, rectangles = TRUE)
    )
    print(p)