我正在使用这个脚本:
# my data:
df <- data.frame(Somatic = c("PIK3CA", "TP53", "GATA3", "KMT2C", "PTEN", "NCOR1", "ARID1A", "RUNX1", "NF1", "TBX3", "FOXA1", "ERBB2", "AKT1", "PREX2", "CBFB", "INPPL1", "NSD3", "ESR1", "AXIN2", "RAD51C"),
Freq_mut = c(32.6, 32.6, 11.9, 9.3, 5.4, 4.7, 3.8, 3.8, 3.6, 3.1, 2.9, 2.8, 2.5, 2.4, 2.3, 1, 1, 0.8, 0.6, 0.6))
library(randomcoloR)
# set of 20 random colors
colors <- distinctColorPalette(20)
ggplot(df, aes(x = "", y = Freq_mut, fill = Somatic)) +
geom_bar(width = 1, stat = "identity", color = "black") +
coord_polar("y", start = 0) +
scale_fill_manual(values = colors) +
ggtitle("A") +
labs(fill = "Somatic Gene") +
theme(panel.background = element_blank(), plot.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
geom_label_repel(data = df,
aes(y = Freq_mut, label = paste0(Somatic)),
size = 4.5, nudge_x = 1, show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)
但正如您在图像中看到的那样,标签的位置不正确并且缺少其余的基因。我该如何修复它并删除图左侧的“x”?谢谢!
我认为你可能最好在这里直接贴标签。请记住,如果您有堆叠条形图,则 y 值会堆叠,因此您的文本标签也需要使用以下方式堆叠position_stack
library(randomcoloR)
library(geomtextpath)
# set of 20 random colors
colors <- distinctColorPalette(20)
ggplot(df, aes(x = 1, y = Freq_mut, fill = Somatic)) +
geom_bar(width = 1, stat = "identity", color = "black") +
coord_polar("y", start = 0) +
scale_fill_manual(values = colors, guide = "none") +
ggtitle("A") +
labs(fill = "Somatic Gene", x = NULL) +
theme(panel.background = element_blank(), plot.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
geom_textpath(data = df, position = position_stack(vjust = 0.5),
aes(x = 1.7, y = Freq_mut, label = paste0(Somatic)),
size = 4.5, show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)
或者
ggplot(df, aes(x = 1, y = Freq_mut, fill = Somatic)) +
geom_bar(width = 1, stat = "identity", color = "black") +
coord_polar("y", start = 0) +
scale_fill_manual(values = colors, guide = "none") +
ggtitle("A") +
labs(fill = "Somatic Gene", x = NULL) +
theme(panel.background = element_blank(), plot.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
geom_labelpath(data = df, position = position_stack(vjust = 0.5),
aes(x = 1.7, y = Freq_mut, label = paste0(Somatic)),
size = 4.5, show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)
要添加百分比,您可以尝试:
ggplot(df, aes(x = 1, y = Freq_mut, fill = Somatic)) +
geom_bar(width = 1, stat = "identity", color = "black") +
coord_polar("y", start = 0, clip = "off") +
scale_fill_manual(values = colors, guide = "none") +
xlim(0.5, 2) +
ggtitle("A") +
labs(fill = "Somatic Gene", x = NULL) +
theme(panel.background = element_blank(), plot.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
geom_textpath(data = df %>%
mutate(perc = scales::percent(Freq_mut/ sum(Freq_mut))),
position = position_stack(vjust = 0.5), hjust = 1,
aes(x = 1.55, y = Freq_mut,
label = paste0(Somatic, " (", perc, ")")),
size = 4.5, show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)