這是我的資料集:
structure(list(title = c("Fried Anchovies with Sage", "Anchovies Appetizer With Breadcrumbs & Scallions",
"Carrots, Cauliflower And Anchovies", "Bap Story: Stir Fried Anchovies (Myulchi Bokkeum)",
"Fried Anchovies", "Tomato & Anchovies With Bread Crumbs, Basil & Red Onion Recip",
"Marinated Fresh Anchovies: Alici Marinate", "Marinated Boquerones",
"Spaghetti With Walnuts And Anchovies", "Roasted Peppers with Boquerones"
), pricePerServing = c(5.6051, 0.8206, 4.38, 8.1122, 1.505, 0.8603,
5.5005, 4.1332, 0.9284, 0.5844), healthScore = c(29, 4, 63, 70,
6, 14, 33, 82, 45, 48), readyInMinutes = c(45L, 15L, 45L, 45L,
15L, 45L, 420L, 45L, 12L, 4500L), veryHealthy = c("False", "False",
"True", "True", "False", "False", "False", "True", "False", "False"
), dairyFree = c("True", "True", "True", "True", "True", "True",
"True", "True", "False", "True"), dishType = c("lunch", "antipasti",
"lunch", "lunch", "antipasti", "side dish", "lunch", "lunch",
"lunch", "side dish"), healthy = c(0.752433090024331, 0.752433090024331,
0.247566909975669, 0.247566909975669, 0.752433090024331, 0.752433090024331,
0.752433090024331, 0.247566909975669, 0.752433090024331, 0.752433090024331
), diary = c(0.423965936739659, 0.423965936739659, 0.423965936739659,
0.423965936739659, 0.423965936739659, 0.423965936739659, 0.423965936739659,
0.423965936739659, 0.576034063260341, 0.423965936739659), percent = c(0.370250606305578,
0.370250606305578, 0.587223587223587, 0.587223587223587, 0.370250606305578,
0.370250606305578, 0.370250606305578, 0.587223587223587, 0.629749393694422,
0.370250606305578)), row.names = c(NA, 10L), class = "data.frame")
我正在嘗試制作百分比堆疊的條形圖,但我無法垂直集中我的文本。position_stack(vjust = 0.5)不起作用。我的代碼是:
ggplot(foods, aes(x=veryHealthy, y=diary, width=healthy , fill = dairyFree,
label = percent))
geom_bar(stat='identity', position="fill")
scale_x_discrete(expand = c(0, 0))
facet_grid(~veryHealthy, scales = "free", space = "free")
geom_text(stat="unique", position = position_stack(vjust = 0.5))
我得到了這個結果:
我如何將 geom_text 輸出集中在這里?
uj5u.com熱心網友回復:
我建議使用匯總表,因為您提供的資料不完整,我創建了一個小樣本匯總表來繪制。您的任務是將資料轉換為如下格式。
foods <- data.frame(
veryHealthy = c(F, F, T, T),
dairyFree = c(T, F, T, F),
percent = c(0.37, 0.63, 0.59, 0.41),
healthy = c(0.75, 0.75, 0.25, 0.25)
)
ggplot(foods, aes(x = veryHealthy, y = percent, width = healthy, fill = dairyFree))
geom_bar(stat='identity', position = "fill")
geom_text(aes(label = percent), position = position_fill(vjust = 0.5))
scale_x_discrete(expand = c(0, 0))
facet_grid(~ veryHealthy, scales = "free", space = "free")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371957.html
上一篇:ggplot生成的是唯一計數的條形圖,而不是每個唯一變數的計數
下一篇:通過按鈕命令添加的標簽不擴展框架
