Advertisements
I am using this script:
# 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)
But as you can see in the image, the labels are not in the correct position and are missing the rest of the genes. How Could I fix it and remove the "x" to the left of the plot?
Thanks!
>Solution :
I think you may be best with direct labelling here. Remember if you have a stacked bar graph, the y values are stacked, so your text labels also need to be stacked using 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)
or
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)