向格子条形图添加标签

发布于 2024-08-19 12:02:35 字数 65 浏览 5 评论 0原文

我想将条形图(格子)中每个条形的值放置在每个条形的顶部。但是,我找不到任何可以实现此目标的选项。我只能找到轴的选项。

I would like to place the value for each bar in barchart (lattice) at the top of each bar. However, I cannot find any option with which I can achieve this. I can only find options for the axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如痴如狂 2024-08-26 12:02:35

创建自定义面板函数,例如

library("lattice")
p <- barchart((1:10)^2~1:10, horiz=FALSE, ylim=c(0,120),
              panel=function(...) { 
                args <- list(...)
                panel.text(args$x, args$y, args$y, pos=3, offset=1)
                panel.barchart(...)
              })
print(p)

lattice barchart with labels

Create a custom panel function, e.g.

library("lattice")
p <- barchart((1:10)^2~1:10, horiz=FALSE, ylim=c(0,120),
              panel=function(...) { 
                args <- list(...)
                panel.text(args$x, args$y, args$y, pos=3, offset=1)
                panel.barchart(...)
              })
print(p)

lattice barchart with labels

少女净妖师 2024-08-26 12:02:35

我建议使用新的 directlabels 包,它可以与网格一起使用和 ggplot (并且使这些标签问题变得非常容易),但不幸的是它不适用于条形图。

I would have suggested using the new directlabels package, which can be used with both lattice and ggplot (and makes life very easy for these labeling problems), but unfortunately it doesn't work with barcharts.

林空鹿饮溪 2024-08-26 12:02:35

因为无论如何我都必须这样做,所以这里有一个足够接近的代码示例,按照 @Alex Brown 的建议(分数是某种二维数组,它将变成分组向量) :

barchart(scores, horizontal=FALSE, stack=FALSE, 
     xlab='Sample', ylab='Mean Score (max of 9)',
     auto.key=list(rectangles=TRUE, points=FALSE),
     panel=function(x, y, box.ratio, groups, errbars, ...) {
         # We need to specify groups because it's not actually the 4th 
         # parameter
         panel.barchart(x, y, box.ratio, groups=groups, ...)
         x <- as.numeric(x)
         nvals <- nlevels(groups)
         groups <- as.numeric(groups)
         box.width <- box.ratio / (1 + box.ratio)
         for(i in unique(x)) {
             ok <- x == i
             width <- box.width / nvals
             locs <- i + width * (groups[ok] - (nvals + 1)/2)
             panel.arrows(locs, y[ok] + 0.5, scores.ses[,i], ...)
         }
     } )

我还没有对此进行测试,但重要的部分(确定面板功能内的位置等的部分)确实有效。这是最难弄清楚的部分。就我而言,我实际上是使用 panel.arrows 来制作错误栏(太恐怖了!)。但scores.ses 是一个与scores 维度相同的数组。

我稍后会尝试清理这个问题 - 但如果其他人愿意,我会很高兴!

Since I had to do this anyway, here's a close-enough-to-figure it out code sample along the lines of what @Alex Brown suggests (scores is a 2D array of some sort, which'll get turned into a grouped vector):

barchart(scores, horizontal=FALSE, stack=FALSE, 
     xlab='Sample', ylab='Mean Score (max of 9)',
     auto.key=list(rectangles=TRUE, points=FALSE),
     panel=function(x, y, box.ratio, groups, errbars, ...) {
         # We need to specify groups because it's not actually the 4th 
         # parameter
         panel.barchart(x, y, box.ratio, groups=groups, ...)
         x <- as.numeric(x)
         nvals <- nlevels(groups)
         groups <- as.numeric(groups)
         box.width <- box.ratio / (1 + box.ratio)
         for(i in unique(x)) {
             ok <- x == i
             width <- box.width / nvals
             locs <- i + width * (groups[ok] - (nvals + 1)/2)
             panel.arrows(locs, y[ok] + 0.5, scores.ses[,i], ...)
         }
     } )

I haven't tested this, but the important bits (the parts determining the locs etc. within the panel function) do work. That's the hard part to figure out. In my case, I was actually using panel.arrows to make errorbars (the horror!). But scores.ses is meant to be an array of the same dimension as scores.

I'll try to clean this up later - but if someone else wants to, I'm happy for it!

贵在坚持 2024-08-26 12:02:35

如果您使用groups 参数,您会发现@rcs 代码中的标签都彼此重叠。这可以通过扩展 panel.text 使其像 panel.barchart 一样工作来修复,如果您了解 R,这很容易。

出于许可原因,我无法在此处发布修复代码,抱歉。

If you are using the groups parameter you will find the labels in @rcs's code all land on top of each other. This can be fixed by extending panel.text to work like panel.barchart, which is easy enough if you know R.

I can't post the code of the fix here for licencing reasons, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文