我的標簽大致與堆積條形圖的每一側對齊。問題是它們看起來一團糟,因為它們在欄的兩側都沒有左右對齊。我如何解決這個問題,讓他們看起來很專業?
df3 <- data.frame(
Label = c("Dasher", "Dancer", "Comet", "Cupid", "Prancer", "Blitzen", "Rudolph"),
Amount = c(650.01, 601.01, 340.05, 330.20, 260.01, 250.80, 10.10)
)
# Sort order
level_order <- df3 %>%
arrange(desc(Amount))
ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x=""))
geom_bar(position="stack", stat="identity", width = 0.55)
scale_fill_brewer(palette = "Blues", direction = -1)
theme_void()
geom_text(aes(label = paste0("$", Amount)),
position = position_stack(vjust = 0.5),
hjust = -3.1,
size = 5)
geom_text(aes(label = Label),
position = position_stack(vjust = 0.5),
hjust = 5,
size = 5)
theme(legend.position = "none")
theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0))
ggtitle("Food Costs by Reindeer")

uj5u.com熱心網友回復:
hjust確定文本對齊方式(0 左對齊,1 右對齊)。你現在的 x 坐標geom_text默認為 1,所以改變它會改變文本的位置。
ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x=""))
geom_bar(position="stack", stat="identity", width = 0.55)
scale_fill_brewer(palette = "Blues", direction = -1)
theme_void()
geom_text(aes(x=0.6, label = paste0("$", Amount)),
position = position_stack(vjust = 0.5),
hjust = 0.5,
size = 5)
geom_text(aes(x=1.4, label = Label),
position = position_stack(vjust = 0.5),
hjust = 0.5,
size = 5)
theme(legend.position = "none")
theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0))
ggtitle("Food Costs by Reindeer")
uj5u.com熱心網友回復:
嘗試在呼叫中修復 x 坐標geom_text并管理與hjust...
df3 <- data.frame(
Label = c("Dasher", "Dancer", "Comet", "Cupid", "Prancer", "Blitzen", "Rudolph"),
Amount = c(650.01, 601.01, 340.05, 330.20, 260.01, 250.80, 10.10)
)
library(ggplot2)
library(dplyr)
library(forcats)
level_order <- df3 %>%
arrange(desc(Amount))
ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x=""))
geom_bar(position="stack", stat="identity", width = 0.55)
scale_fill_brewer(palette = "Blues", direction = -1)
theme_void()
geom_text(aes(x = 1.3, label = paste0("$", Amount)),
position = position_stack(vjust = 0.5),
hjust = 0,
size = 5)
geom_text(aes(x = 0.6, label = Label),
position = position_stack(vjust = 0.5),
hjust = 0,
size = 5)
theme(legend.position = "none")
theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0))
ggtitle("Food Costs by Reindeer")

由
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387221.html
下一篇:帶有面板資料的條形圖
