调整coord_polar()直方图中文本标签的位置 [英] Adjusting position of text labels in coord_polar() histogram
问题描述
我被困在一个小的标签问题上,它有一系列在ggplot2中制作的极坐标直方图(circumplexes?这些东西是怎么称呼的?)。
这是一个数据和图形如何显示的简单示例:
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
其中给出以下图表:
我想将文本标签向外移动(远离中心)。
通常情况下,我会用 hjust
或 vjust
内部 geom_text()
,但看起来, code> coord_polar(),结果是向上/向下或向左/向右移动所有标签,但不在/向外。
这听起来很平凡 - 可能是 - 但我还没有找到任何适用的例子或解决方法,所以我很抱歉,如果这个问题看起来很愚蠢。
我假设您将数字值作为标签引用,并且您希望它们移动一点(与属性1文本相对)。
您可以将一些美学贴图移动到 geom_text
调用并向y值添加一个小值:
g < - ggplot(melt.data (x =变量,y =值,fill =变量))
g <-g + geom_bar()+ geom_text(aes(y = value + 0.5,label = value))+ coord_polar()
g
I'm stuck on an small labeling issue with a series of polar histograms made in ggplot2 (circumplexes? how are these things called?).
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:
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.
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.
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).
You can just move some of the aesthetic mapping to the geom_text
call and add a small value to the y values:
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()直方图中文本标签的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!