我有一個 100% 堆積條形圖,顯示 3 種型別的變數。我已經設定了一個示例資料庫,以便我可以更輕松地創建圖表。
我已經用我需要的顏色和資訊調整了圖表。但我無法獨立定位標簽。這是當前的代碼和輸出。
代碼:
(empilhado<-ggplot(dfm, aes(y = Year, x = abs(value), fill = variable))
scale_x_continuous(sec.axis = sec_axis(trans = ~.*1, name="Trab."), expand=expansion(mult=c(0,0.05)))
geom_col(data = rotulo, aes(y = Year, x=abs(trabalho), fill=NULL), width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y")
geom_label(data = rotulo, aes(y= Year, x = abs(trabalho), fill=NULL, label=paste(format(round(trabalho, digits=0), nsmall=0, decimal.mark=",", big.mark="."),
format(round(aprovado, digits=0), nsmall=0, decimal.mark=",", big.mark="."))
), color="black", size=4, position = position_fill(vjust=1.06))
geom_col(width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y")
geom_text(aes(label=format(round(value, digits=0), nsmall=0, decimal.mark=",", big.mark=".")),
size=4, color="white", position = position_fill(vjust=0.5))
theme(panel.grid.major = element_line(colour = "gray90",size=0.75), panel.grid.minor = element_line(colour = "gray90",size=0.75),
legend.position="top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.x = element_blank(), panel.background = element_blank())
scale_fill_manual(values = c("#000000","tomato","blue"))
輸出:

現在怎么樣了?Position_fill(vjust=0.5),因此所有標簽都在其各自的條形圖中居中。
我想要的是?為了能夠在左側設定 'Marionete' 標簽的位置(就像 vjust=0 那樣),保持 'Pedido' 標簽原樣(在 'Pedido' 堆疊條的中心)并放置 ' Fatura' 標簽在右邊(就像 vjust=1 那樣)。
提前致謝!
uj5u.com熱心網友回復:
實作您想要的結果的一種選擇是手動計算/設定每個標簽的位置和水平對齊,而不是使用position="fill":
利用一些隨機模擬資料:
library(ggplot2)
library(dplyr)
dfm <- dfm %>%
group_by(Year) %>%
arrange(desc(variable)) %>%
mutate(
pct = value / sum(value),
x_label = case_when(
variable == "Marionete" ~ 0,
variable == "Pedido" ~ .5 * (cumsum(pct) lag(cumsum(pct))),
TRUE ~ 1
),
hjust = case_when(
variable == "Marionete" ~ 0,
variable == "Pedido" ~ .5,
TRUE ~ 1
)
)
ggplot(dfm, aes(y = Year, x = abs(value), fill = variable))
scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 1, name = "Trab."), expand = expansion(mult = c(0, 0.05)))
geom_col(width = .7, colour = "black", lwd = 0.1, position = "fill", orientation = "y")
geom_text(aes(x = x_label, label = format(round(value, digits = 0), nsmall = 0, decimal.mark = ",", big.mark = "."), hjust = hjust),
size = 4, color = "white"
)
theme(
panel.grid.major = element_line(colour = "gray90", size = 0.75), panel.grid.minor = element_line(colour = "gray90", size = 0.75),
legend.position = "top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.x = element_blank(), panel.background = element_blank()
)
scale_fill_manual(values = c("#000000", "tomato", "blue"))

資料
set.seed(123)
dfm <- data.frame(
Year = rep(c(2006:2016), each = 3),
value = sample(1:100, 3 * 11, replace = TRUE),
variable = c("Fatura", "Pedido", "Marionete")
)
dfm$variable <- factor(dfm$variable, levels = c("Fatura", "Pedido", "Marionete"))
dfm$Year <- factor(dfm$Year)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371954.html
上一篇:缺失資料的時間序列圖
