我的資料框
dput(d)
structure(list(x = c("Organonitrogen compound metabolic process",
"Cellular process", "Nitrogen compound metabolic process", "Primary metabolic process",
"Organic substance biosynthetic process", "Metabolic process",
"Cellular nitrogen compound biosynthetic process", "Cellular metabolic process",
"Organic substance metabolic process", "Cellular biosynthetic process"
), freq = c(71, 119, 87, 89, 52, 94, 42, 89, 89, 49)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我正在運行以生成餅圖的代碼
d$perc <- round(100 * d$freq / sum(d$freq))
ggplot(data = d, aes(x = 0, y = freq, fill = x))
geom_bar(stat = "identity")
geom_text(aes(label = perc), position = position_stack(vjust = 0.5))
scale_x_continuous(expand = c(0,0))
labs(fill = 'Type', x = NULL, y = NULL, title = 'Pathway Pie chart', subtitle = 'percentages')
coord_polar(theta = "y")
theme_minimal()
我得到這樣的東西

但我想看到這樣的東西。
任何幫助或建議將不勝感激

uj5u.com熱心網友回復:
scales::percent()是為此而設計的。嘗試
geom_text(aes(label = scales::percent(freq/sum(freq), 1)),
position = position_stack(vjust = 0.5))
因為你已經將百分比計算為perc,所以可以直接傳入percent()。(注意它的 argsaccuracy和scale。在你的情況下,它們都應該是 1)
geom_text(aes(label = scales::percent(perc, 1, 1)),
position = position_stack(vjust = 0.5))
您可以搜索?scales::percent以查看更多要調整的引數。
uj5u.com熱心網友回復:
您可以使用theme_void洗掉軸和刻度并sprintf顯示百分比符號。
ggplot(data = d, aes(x = 0, y = freq, fill = x))
geom_bar(stat = "identity")
geom_text(aes(label = sprintf("%d%%",perc)),
position = position_stack(vjust = 0.5),
size = 3)
scale_x_continuous(expand = c(0,0))
labs(fill = 'Type', x = NULL, y = NULL, title = 'Pathway Pie chart', subtitle = 'percentages')
coord_polar(theta = "y")
theme_void()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462752.html
下一篇:R:如何按方差排序箱線圖?
