[英]How to vertically center labels with geom_bar (ggplot2)
我將非常感謝 ggplot2 的一些幫助。 我似乎無法將條形上的標簽垂直居中。
這是代碼:
library(ggplot2)
Group.id <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Type.id <- c("a", "b", "c", "d", "a", "b", "c", "d","a", "b", "c", "d")
Value <- c(4000, 3200, 9529, 3984, 7504, 1244, 8960, 1865, 1100, 1100, 0, 0)
df <- data.frame(Group.id, Type.id, Value)
ggplot(df, aes(x = Group.id, y = Value, label = Value)) +
geom_bar(stat = "identity", aes(fill = Type.id)) +
scale_fill_manual(values=c("#00AF50", "#64A70B", "#F2A900", "#C30C3E"), labels = rev(unique(df$Type.id))) +
geom_text(position = position_stack(vjust = .5), color = "#FFFFFF")
結果如下:沒有居中標簽的條形圖
它適用於較小的值,但由於某種原因,它不以較大的值為中心。 有什么建議嗎?
將fill = Type.id
放在第一個aes()
調用中:
ggplot(df, aes(x = Group.id, y = Value, label = Value, fill = Type.id)) +
geom_bar(stat = "identity") +
scale_fill_manual(values=c("#00AF50", "#64A70B", "#F2A900", "#C30C3E"), labels = rev(unique(df$Type.id))) +
geom_text(position = position_stack(vjust = .5), color = "#FFFFFF")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.