我有这样的样本数据:
name \<- rep(c("ba", "EW", "RW", "Se", "St", "US", "VN"), 2)
value \<- c(0, 6323, 7397, 13945, 11801, 55255, 22519, 4124, 13540, 9616, 57724, 6646, 22021, 8841)
type \<- c(rep("g", 7), rep("o", 7))
test \<- data.frame(name, value, type)
并使用代码创建了一个绘图:
ggplot(test, aes(x=name, y=value, fill=type)) +
geom_bar(position = position_stack(reverse = TRUE), stat='identity') +
theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#0099f9", "#69c6ff")) +
geom_text(aes(label = value), position = position_stack(vjust = 0.5), colour = "black", size = 3) +
guides(fill = guide_legend(title = "", reverse = TRUE))
数据标签不在每个块中居中: 情节
我使用了在ggplot2中的堆积条形图上显示数据值和(R) ggplot2标签未正确居中中的解决方案,但是
position = position_stack(vjust = 0.5)
在 geom_text 中没有帮助
为什么会这样,我还能做什么?
您也必须
reverse
标签的堆栈顺序,即使用position_stack(vjust = 0.5, reverse = TRUE)
作为geom_text
:
library(ggplot2)
ggplot(test, aes(x = name, y = value, fill = type)) +
geom_bar(position = position_stack(reverse = TRUE), stat = "identity") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#0099f9", "#69c6ff")) +
geom_text(aes(label = value),
position = position_stack(vjust = 0.5, reverse = TRUE),
colour = "black", size = 3
) +
guides(fill = guide_legend(title = "", reverse = TRUE))