我正在嘗試使用 ggplot 2 繪制甜甜圈圖。您可以在下面看到我的資料以及我的圖。
test_data<-structure(list(KindOfParticipants = c("Participants", "Non-participants",
"Unknown group"), variable = structure(c(1L, 1L, 1L), .Label = "Total", class = "factor"),
value = c(111L, 5937L, 18L), fraction = c(0.0431091877496671,
0.953894806924101, 0.00299600532623169), ymax = c(0.0431091877496671,
0.997003994673768, 1), ymin = c(0, 0.0431091877496671, 0.997003994673768
), labelPosition = c(0.0215545938748336, 0.520056591211718,
0.998501997336884), label = c("Participants\n value: 111", "Non-Participants\n value: 5937",
"Unknown group\n value: 110")), row.names = c(NA, -3L), class = "data.frame")
現在我想用這個資料繪制甜甜圈圖。
plot_1_test<-ggplot(test_data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=KindOfParticipants))
geom_rect()
geom_text( x=1, aes(y=labelPosition, label=label, color=variable), size=6) # x here controls label position (inner / outer)
scale_fill_brewer(palette=3)
scale_color_brewer(palette=3)
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test
但是這些代碼行給了我一個甜甜圈圖,如下圖:

所以有了這個情節,我有兩個問題主要與圓圈內的文字有關
首先我想在圓圈內有可讀的文本,現在有文本重疊,其次是在圓圈內有較深的顏色。
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
hjust你可以像下面這樣調整你的標簽。
require(ggplot2)
plot_1_test <- ggplot(test_data, aes(ymax=ymax, ymin=ymin,
xmax=4, xmin=3,
fill=KindOfParticipants))
geom_rect()
# geom_label(x=3.5,aes(y=labelPosition, label=label),hjust=c(0,1,2))
geom_text(x=1, aes(y=labelPosition, label=label,
color=KindOfParticipants),
size=5,
hjust=c(-0.2,0.5,1)) # x here controls label position (inner / outer)
scale_fill_brewer(palette=3)
scale_color_brewer(palette=3)
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test

由reprex 包于 2022-05-03 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471620.html
下一篇:ggplotx軸標簽無法編輯
