向格子条形图添加标签
我想将条形图(格子)中每个条形的值放置在每个条形的顶部。但是,我找不到任何可以实现此目标的选项。我只能找到轴的选项。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建自定义面板函数,例如
Create a custom panel function, e.g.
我建议使用新的 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.
因为无论如何我都必须这样做,所以这里有一个足够接近的代码示例,按照 @Alex Brown 的建议(分数是某种二维数组,它将变成分组向量) :
我还没有对此进行测试,但重要的部分(确定面板功能内的位置等的部分)确实有效。这是最难弄清楚的部分。就我而言,我实际上是使用 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):
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!
如果您使用
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.