调整coord_polar()直方图中文本标签的位置

时间:2023-02-01 13:46:28

I'm stuck on an small labeling issue with a series of polar histograms made in ggplot2 (circumplexes? how are these things called?).

我在ggplot2中制作了一系列极性直方图,这是一个很小的标签问题(环形?这些东西怎么称呼?)。

Here is a simplified example of how the data and the graph look:

以下是数据和图形外观的简化示例:

df <- data.frame(Attribute1=10, Attribute2=1, Attribute3=2,  Attribute4=6,  Attribute5=7)
g <- ggplot(melt.data.frame(df), aes(x=variable, y=value, fill=variable, label=value))
g <- g + geom_bar() + geom_text() + coord_polar()
g

Which gives the following graph: 调整coord_polar()直方图中文本标签的位置

其中给出了以下图表:

I would like to move the text labels outwards (away from the center).

我想向外移动文本标签(远离中心)。

Normally, I would adjust the position with hjust or vjust inside geom_text(), but it seems that, with coord_polar(), the result is to move all the labels up/downwards or left/rightwards, but not in/outwards.

通常情况下,我会在geom_text()中使用hjust或vjust调整位置,但似乎使用coord_polar(),结果是向上/向下或向左/向右移动所有标签,但不是向内/向外移动。

This may sound trivial - and probably is - but I haven't found any applicable example or workaround yet, so I apologize if this question looks silly.

这可能听起来微不足道 - 可能是 - 但我还没有找到任何适用的例子或解决方法,所以如果这个问题看起来很愚蠢我会道歉。

1 个解决方案

#1


11  

I'm assuming that you're referring to the numeric values as labels, and that you want them moved a little outside the pie wedges (as opposed to the "Attribute 1" text).

我假设您将数值称为标签,并且您希望它们在饼形楔形之外移动一点(而不是“属性1”文本)。

You can just move some of the aesthetic mapping to the geom_text call and add a small value to the y values:

您可以将一些美学映射移动到geom_text调用,并为y值添加一个小值:

g <- ggplot(melt.data.frame(df), aes(x=variable, y=value, fill=variable))
g <- g + geom_bar() + geom_text(aes(y = value + 0.5,label = value)) + coord_polar()
g

调整coord_polar()直方图中文本标签的位置

#1


11  

I'm assuming that you're referring to the numeric values as labels, and that you want them moved a little outside the pie wedges (as opposed to the "Attribute 1" text).

我假设您将数值称为标签,并且您希望它们在饼形楔形之外移动一点(而不是“属性1”文本)。

You can just move some of the aesthetic mapping to the geom_text call and add a small value to the y values:

您可以将一些美学映射移动到geom_text调用,并为y值添加一个小值:

g <- ggplot(melt.data.frame(df), aes(x=variable, y=value, fill=variable))
g <- g + geom_bar() + geom_text(aes(y = value + 0.5,label = value)) + coord_polar()
g

调整coord_polar()直方图中文本标签的位置