英文:
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_text
中aes
函数中的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 <- 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))
答案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()
我还想指出,从数据可视化和美学角度来看,在这里使用堆叠的单一条是不是展示数据的最佳方式。比较一下,例如:
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')
这种方式更整洁,更易读,更容易解释,看起来也更好看。
从问题中的图像推断的数据近似值
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 = "identity")
is just a long way of writing 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()
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 %>%
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')
Which is tidier, easier to read, easier to interpret and nicer to look at.
Data approximation inferred from image in question
sk <- data.frame(A = 38, B = 226, C = 752, D = 1692, E = 3045, F = 5977)/100
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论