[英]ggplot2: geom_bar and position_dodge: not centered to x axis (factor)
I have two issue on this plot.我对这个情节有两个问题。 I want to make the bars wider (and less spacing between the groups) and I want each group of bars to be centered to each x factor values.
我想让条形更宽(组之间的间距更小)并且我希望每组条形都以每个 x 因子值为中心。
I have a continuous variable on the y-axis and a factor value on the x-axis.我在 y 轴上有一个连续变量,在 x 轴上有一个因子值。 I have three groups for each factor.
每个因素我有三组。
Here is an example of my issue with the Iris data:以下是我的 Iris 数据问题示例:
d <- iris
ggplot(d) +
geom_col(aes(x=as.factor(Sepal.Length), y=Petal.Width, fill=as.factor(Species)),position = position_dodge(preserve = "single"), width=1) +
theme(axis.text.x = element_text(angle = 90).
This gets you something probably closer to what you're looking for (I assume d
in your example is iris
):这让你可能更接近你正在寻找的东西(我假设你的例子中的
d
是iris
):
ggplot(iris) +
geom_col(
aes(
x=as.factor(Sepal.Length), y=Petal.Width, fill=Species
),
position = position_dodge(0.5),
width=0.5) +
theme(axis.text.x = element_text(angle = 90, vjust=0.5))
Now, for the explanation of what I changed and why:现在,解释我改变了什么以及为什么:
Text Positioning on X-axis. X 轴上的文本定位。 You used
element_text(angle=90)
to change the direction of the text.您使用
element_text(angle=90)
来更改文本的方向。 This is correct, but it only changes the angle and not the positioning/alignments .这是正确的,但它只会改变角度而不是定位/对齐。 By default, horizontal text is vertically aligned to be "at the top".
默认情况下,水平文本垂直对齐为“在顶部”。 If you run the code above and use
vjust=1
in place of vjust=0.5
, you'll see it goes back to the way it appears for you, with the tick marks being aligned to the "top" of the value on the x axis text.如果您运行上面的代码并使用
vjust=1
代替vjust=0.5
,您会看到它恢复为您显示的方式,刻度线与 x 上的值的“顶部”对齐轴文本。
as.factor(Species) No need to declare Species
a factor. as.factor(Species)无需将
Species
声明为因子。 Run str(iris)
and you'll see that iris$Species
is already a factor.运行
str(iris)
,您会看到iris$Species
已经是一个因素。 Doesn't really change anything to the result except messes with the title of the legend.除了弄乱图例的标题之外,并没有真正改变任何结果。
Position_dodge width and width. Position_dodge 宽度和宽度。 This one is best explained by you messing with the values in the two terms
position_dodge(0.5)
and width=0.5
.最好通过您弄乱
position_dodge(0.5)
和width=0.5
两个术语中的值来解释这一点。 Play with it yourself and you'll see what they each do, but here's the general explanation:自己玩一下,你会看到他们每个人都做了什么,但这里是一般解释:
Total column width for each position on the x-axis is determined by width=0.5
that is the argument for geom_col()
. x 轴上每个位置的总列宽由
width=0.5
确定,这是geom_col()
的参数。 So, for every Sepal.Length
factor in this graph, it means that "0.5" is used as the total width of the column (or columns) that are in that space.因此,对于此图中的每个
Sepal.Length
因子,这意味着“0.5”用作该空间中一列(或多列)的总宽度。 "1.0" would mean "I want all columns to touch each other" and something like "0.2" means "I want skinny columns". “1.0”表示“我希望所有列相互接触”,而“0.2”表示“我想要细列”。 "0" means... I don't want columns - give them a width of zero!
“0”意味着......我不想要列 - 给它们一个宽度为零!
The width of each "sub-column" (each Species column for each Sepal.Length in this example) is controlled by the position_dodge(width=0.5)
term.每个“子列”的宽度(在这个例子中每个 Sepal.Length 的每个 Species 列)由
position_dodge(width=0.5)
项控制。 0.5 represents "split this in half and have the columns touch each other exactly". 0.5 表示“将其分成两半并使列彼此完全接触”。 Higher values will split them apart and lower values will squish them together, where 0 means they are on top of one another.
较高的值会将它们分开,较低的值会将它们挤压在一起,其中 0 表示它们位于彼此之上。 Making the value really large, you get sub-columns running into neighboring columns...
使该值变得非常大,您会得到子列运行到相邻列中......
Again - play around with those terms and you should get how they work together.再次 - 使用这些术语,您应该了解它们如何协同工作。
Maybe an another solution is to use position_dodge2
that will center each bar to the center of x values:也许另一种解决方案是使用
position_dodge2
将每个条形居中到 x 值的中心:
ggplot(iris, aes(x = as.factor(Sepal.Length), y = Petal.Width, fill = Species))+
geom_col(position = position_dodge2(preserve = "single", width = 1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.