在ggplot2中,如何在单个堆叠条形图的每个子部分中垂直居中数值?

huangapple go评论69阅读模式
英文:

How to center value vertically on each subsection of a single stacked bar plot in ggplot2?

问题

我想创建一个堆叠条形图,只有一个条,我希望每个值都显示在图上,位于相应部分的中间。

这是我的代码:

sk <- sk %>%
  pivot_longer(cols = c(A, B, C, D, E, F), 
               names_to = "Variable", values_to = "Value")

sk <- sk %>%
  group_by(Variable) %>%
  mutate(cumulative = cumsum(Value) - (Value / 2))

ggplot(data = sk, aes(x = variable, y = Value, fill = Variable)) +
  geom_bar(position = "stack", stat = "identity") +
  xlab("") + ylab("") +
  theme_minimal() +
  theme(legend.position = "right") +
  geom_text(aes(y = cumulative, label = Value))

你的代码看起来没问题。如果你想移动文本标签,可以尝试调整geom_textaes函数中的y值,以更改文本标签的位置。如果标签颠倒了,你可以使用angle参数旋转它们,或者调整数据的顺序以确保它们按照你想要的方式堆叠在一起。

英文:

I want to create a stacked bar plot with a single bar, and I would like that each value is shown on the graph, in the middle of the corresponding section.

This enter image description hereis what I get and I have no idea on how to move them. It's weird because they are upside down too.

This is my code:

sk &lt;- sk %&gt;% 
  pivot_longer(cols = c(A, B,C, D, E, F), 
               names_to = &quot;Variable&quot;, values_to = &quot;Value&quot;)

sk &lt;- sk %&gt;%
  group_by(Variable) %&gt;%
  mutate(cumulative = cumsum(Value) - (Value / 2))

ggplot(data = sk, aes(x = variable, y = Value, fill = Variable)) +
  geom_bar(position = &quot;stack&quot;, stat = &quot;identity&quot;) +
  xlab(&quot;&quot;) + ylab(&quot;&quot;) +
  theme_minimal() +
  theme(legend.position = &quot;right&quot;) +
geom_text(aes(y = cumulative, label = Value))

答案1

得分: 1

以下是翻译好的内容:

"你不需要尝试计算堆叠标签的累积位置。只需在你的 geom_text 调用中添加 position = position_stack(vjust = 0.5) 即可。

还请注意,geom_bar(stat = "identity") 只是写作 geom_col 的一种冗长方式:"

library(tidyverse)

sk %>%
  pivot_longer(cols = c(A, B, C, D, E, F), 
               names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = "", y = Value, fill = Variable)) +
  geom_col(position = "stack") +
  geom_text(position = position_stack(vjust = 0.5), aes(label = Value)) +
  labs(x = NULL, y = NULL) +
  theme_minimal() 

在ggplot2中,如何在单个堆叠条形图的每个子部分中垂直居中数值?

我还想指出,从数据可视化和美学角度来看,在这里使用堆叠的单一条是不是展示数据的最佳方式。比较一下,例如:

sk %>%
  pivot_longer(cols = c(A, B, C, D, E, F), 
               names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Variable, y = Value, fill = Variable)) +
  geom_col() +
  geom_text(aes(label = Value), nudge_y = 2) +
  labs(x = NULL, y = NULL) +
  theme_minimal() +
  theme(legend.position = 'none')

在ggplot2中,如何在单个堆叠条形图的每个子部分中垂直居中数值?

这种方式更整洁,更易读,更容易解释,看起来也更好看。


从问题中的图像推断的数据近似值

sk <- data.frame(A = 38, B = 226, C = 752, D = 1692, E = 3045, F = 5977)/100
英文:

You don't need to try to work out the cumulative position of the stacked labels. Simply put position = position_stack(vjust = 0.5) inside your geom_text call.

Also note that geom_bar(stat = &quot;identity&quot;) is just a long way of writing geom_col:

library(tidyverse)

sk %&gt;% 
  pivot_longer(cols = c(A, B, C, D, E, F), 
               names_to = &quot;Variable&quot;, values_to = &quot;Value&quot;) %&gt;%
  ggplot(aes(x = &quot;&quot;, y = Value, fill = Variable)) +
  geom_col(position = &quot;stack&quot;) +
  geom_text(position = position_stack(vjust = 0.5), aes(label = Value)) +
  labs(x = NULL, y = NULL) +
  theme_minimal() 

在ggplot2中,如何在单个堆叠条形图的每个子部分中垂直居中数值?

I might also point out that from both a data viz and aesthetic point of view, a stacked single bar isn't the best way to show your data here. Compare, for example:

sk %&gt;% 
  pivot_longer(cols = c(A, B, C, D, E, F), 
               names_to = &quot;Variable&quot;, values_to = &quot;Value&quot;) %&gt;%
  ggplot(aes(x = Variable, y = Value, fill = Variable)) +
  geom_col() +
  geom_text(aes(label = Value), nudge_y = 2) +
  labs(x = NULL, y = NULL) +
  theme_minimal() +
  theme(legend.position = &#39;none&#39;)

在ggplot2中,如何在单个堆叠条形图的每个子部分中垂直居中数值?

Which is tidier, easier to read, easier to interpret and nicer to look at.


Data approximation inferred from image in question

sk &lt;- data.frame(A = 38, B = 226, C = 752, D = 1692, E = 3045, F = 5977)/100

huangapple
  • 本文由 发表于 2023年7月6日 20:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定