[英]R ggplot2: labels inside bars, no stacked geom_bar
我有以下數據集:
data <- structure(list(Q14 = c("< 5 people", "> 11 people", "6-10 people",
NA), count = c(148L, 13L, 34L, 21L), var = c("Team Size", "Team Size",
"Team Size", "Team Size")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
而我 plot 我的 geom_bar 如下:
library(ggplot2)
library(wesanderson)
ggplot(data) +
geom_bar( aes(x = var, y = count, fill = Q14), stat = "identity", position = "fill") +
coord_flip() +
theme(legend.position = "none",
axis.title.x=element_blank(), axis.title.y=element_blank()) +
scale_fill_manual(values = wes_palette("Zissou1", 3, type = "continuous"))
我想在欄內打印標簽,如下所示。 注意:我的編輯技巧很爛,我當然希望標簽對齊,它們也可以逆時針旋轉。
另一種選擇是獲得以下內容,我也喜歡:
一種選擇是使用geom_text
:
ggplot(data, aes(x = var, y = count, fill = Q14, label = Q14)) +
geom_bar(stat = "identity", position = "fill", ) +
geom_text(position = position_fill(vjust = 0.5), size = 3) +
coord_flip() +
theme(legend.position = "none",
axis.title.x=element_blank(),
axis.title.y=element_blank()) +
scale_fill_manual(values = wes_palette("Zissou1", 3, type = "continuous"))
另一種選擇是使用geom_label_repel
的ggrepel
:
library(ggrepel)
ggplot(data, aes(x = var, y = count, fill = Q14, label = Q14)) +
geom_bar(stat = "identity", position = "fill", ) +
geom_label_repel(position = position_fill(vjust = 0.5),
direction = "y",
point.padding = 1,
segment.size = 0.2,
size = 3,
seed = 3) +
coord_flip() +
theme(legend.position = "none",
axis.title.x=element_blank(),
axis.title.y=element_blank()) +
scale_fill_manual(values = wes_palette("Zissou1", 3, type = "continuous"))
注意seed
參數設置每個label走向哪個方向的隨機過程。 如果您不喜歡我喜歡的號碼,請選擇其他號碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.