Adding % labels to ggplot2 multi-pie chart












0















I have created some nice pie charts but am having difficulty adding % labels to the pie charts. Environment is Linux.



Input data is a tab delimited text file:



TIMEFRAME   POPULATION  AMOUNT
Deepest_Ancestral African 0.06
Deepest_Ancestral East_Asian 0.23
Deepest_Ancestral European 0.71
Deeper_Ancestral African 0.00
Deeper_Ancestral East_Asian 0.40
Deeper_Ancestral European 0.60
Ancestral African 0.00
Ancestral East_Asian 0.10
Ancestral European 0.90


MY CODE:



library(ggplot2)
library(dplyr)

file_name <- "X3.txt"

#load file into data frame
test <- read.csv(file_name, sep="t", header = TRUE)

ggsave("MultiPie.png")


ggplot(test, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
geom_bar(width = 1, size = 0.5, color = "white", stat = "identity") +
geom_text(aes(label = AMOUNT), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_wrap(~ TIMEFRAME, nrow = 2, ncol = 2) +
ggtitle("MUTATIONS YOU SHARE WITH VARIOUS POPULATIONSnn") +
theme(plot.title = element_text(family = "Arial", color="black", face="bold", size=12, hjust=0.5)) +
theme(legend.title = element_text(family = "Arial", color="black", face="bold", size=10, hjust=0)) +
scale_fill_manual(values = c("red4", "gold1", "blue2")) +
scale_color_manual(values = c("red4", "gold1", "blue2")) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.background = element_rect(fill = "gray80"),
plot.background = element_rect(fill = "gray70"),
panel.background = element_rect(fill = "grey70"),
legend.position = "bottom", legend.justification = "center")

dev.off()


THE OUTPUT:



Multi-Pie



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx










share|improve this question























  • check out this answer in SO. It might be what you're looking for

    – Wally Ali
    Nov 21 '18 at 4:03






  • 2





    Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

    – atsyplenkov
    Nov 21 '18 at 10:39











  • I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

    – Gene100
    Nov 21 '18 at 11:18






  • 1





    The rows of xxxx do give away the attention to detail and style.

    – hrbrmstr
    Nov 21 '18 at 12:54











  • @Gene100: see also this speakerdeck.com/cherdarchuk/…

    – Tung
    Nov 21 '18 at 13:21
















0















I have created some nice pie charts but am having difficulty adding % labels to the pie charts. Environment is Linux.



Input data is a tab delimited text file:



TIMEFRAME   POPULATION  AMOUNT
Deepest_Ancestral African 0.06
Deepest_Ancestral East_Asian 0.23
Deepest_Ancestral European 0.71
Deeper_Ancestral African 0.00
Deeper_Ancestral East_Asian 0.40
Deeper_Ancestral European 0.60
Ancestral African 0.00
Ancestral East_Asian 0.10
Ancestral European 0.90


MY CODE:



library(ggplot2)
library(dplyr)

file_name <- "X3.txt"

#load file into data frame
test <- read.csv(file_name, sep="t", header = TRUE)

ggsave("MultiPie.png")


ggplot(test, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
geom_bar(width = 1, size = 0.5, color = "white", stat = "identity") +
geom_text(aes(label = AMOUNT), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_wrap(~ TIMEFRAME, nrow = 2, ncol = 2) +
ggtitle("MUTATIONS YOU SHARE WITH VARIOUS POPULATIONSnn") +
theme(plot.title = element_text(family = "Arial", color="black", face="bold", size=12, hjust=0.5)) +
theme(legend.title = element_text(family = "Arial", color="black", face="bold", size=10, hjust=0)) +
scale_fill_manual(values = c("red4", "gold1", "blue2")) +
scale_color_manual(values = c("red4", "gold1", "blue2")) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.background = element_rect(fill = "gray80"),
plot.background = element_rect(fill = "gray70"),
panel.background = element_rect(fill = "grey70"),
legend.position = "bottom", legend.justification = "center")

dev.off()


THE OUTPUT:



Multi-Pie



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx










share|improve this question























  • check out this answer in SO. It might be what you're looking for

    – Wally Ali
    Nov 21 '18 at 4:03






  • 2





    Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

    – atsyplenkov
    Nov 21 '18 at 10:39











  • I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

    – Gene100
    Nov 21 '18 at 11:18






  • 1





    The rows of xxxx do give away the attention to detail and style.

    – hrbrmstr
    Nov 21 '18 at 12:54











  • @Gene100: see also this speakerdeck.com/cherdarchuk/…

    – Tung
    Nov 21 '18 at 13:21














0












0








0








I have created some nice pie charts but am having difficulty adding % labels to the pie charts. Environment is Linux.



Input data is a tab delimited text file:



TIMEFRAME   POPULATION  AMOUNT
Deepest_Ancestral African 0.06
Deepest_Ancestral East_Asian 0.23
Deepest_Ancestral European 0.71
Deeper_Ancestral African 0.00
Deeper_Ancestral East_Asian 0.40
Deeper_Ancestral European 0.60
Ancestral African 0.00
Ancestral East_Asian 0.10
Ancestral European 0.90


MY CODE:



library(ggplot2)
library(dplyr)

file_name <- "X3.txt"

#load file into data frame
test <- read.csv(file_name, sep="t", header = TRUE)

ggsave("MultiPie.png")


ggplot(test, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
geom_bar(width = 1, size = 0.5, color = "white", stat = "identity") +
geom_text(aes(label = AMOUNT), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_wrap(~ TIMEFRAME, nrow = 2, ncol = 2) +
ggtitle("MUTATIONS YOU SHARE WITH VARIOUS POPULATIONSnn") +
theme(plot.title = element_text(family = "Arial", color="black", face="bold", size=12, hjust=0.5)) +
theme(legend.title = element_text(family = "Arial", color="black", face="bold", size=10, hjust=0)) +
scale_fill_manual(values = c("red4", "gold1", "blue2")) +
scale_color_manual(values = c("red4", "gold1", "blue2")) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.background = element_rect(fill = "gray80"),
plot.background = element_rect(fill = "gray70"),
panel.background = element_rect(fill = "grey70"),
legend.position = "bottom", legend.justification = "center")

dev.off()


THE OUTPUT:



Multi-Pie



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx










share|improve this question














I have created some nice pie charts but am having difficulty adding % labels to the pie charts. Environment is Linux.



Input data is a tab delimited text file:



TIMEFRAME   POPULATION  AMOUNT
Deepest_Ancestral African 0.06
Deepest_Ancestral East_Asian 0.23
Deepest_Ancestral European 0.71
Deeper_Ancestral African 0.00
Deeper_Ancestral East_Asian 0.40
Deeper_Ancestral European 0.60
Ancestral African 0.00
Ancestral East_Asian 0.10
Ancestral European 0.90


MY CODE:



library(ggplot2)
library(dplyr)

file_name <- "X3.txt"

#load file into data frame
test <- read.csv(file_name, sep="t", header = TRUE)

ggsave("MultiPie.png")


ggplot(test, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
geom_bar(width = 1, size = 0.5, color = "white", stat = "identity") +
geom_text(aes(label = AMOUNT), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_wrap(~ TIMEFRAME, nrow = 2, ncol = 2) +
ggtitle("MUTATIONS YOU SHARE WITH VARIOUS POPULATIONSnn") +
theme(plot.title = element_text(family = "Arial", color="black", face="bold", size=12, hjust=0.5)) +
theme(legend.title = element_text(family = "Arial", color="black", face="bold", size=10, hjust=0)) +
scale_fill_manual(values = c("red4", "gold1", "blue2")) +
scale_color_manual(values = c("red4", "gold1", "blue2")) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.background = element_rect(fill = "gray80"),
plot.background = element_rect(fill = "gray70"),
panel.background = element_rect(fill = "grey70"),
legend.position = "bottom", legend.justification = "center")

dev.off()


THE OUTPUT:



Multi-Pie



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx







r ggplot2 label pie-chart






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 3:26









Gene100Gene100

187




187













  • check out this answer in SO. It might be what you're looking for

    – Wally Ali
    Nov 21 '18 at 4:03






  • 2





    Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

    – atsyplenkov
    Nov 21 '18 at 10:39











  • I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

    – Gene100
    Nov 21 '18 at 11:18






  • 1





    The rows of xxxx do give away the attention to detail and style.

    – hrbrmstr
    Nov 21 '18 at 12:54











  • @Gene100: see also this speakerdeck.com/cherdarchuk/…

    – Tung
    Nov 21 '18 at 13:21



















  • check out this answer in SO. It might be what you're looking for

    – Wally Ali
    Nov 21 '18 at 4:03






  • 2





    Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

    – atsyplenkov
    Nov 21 '18 at 10:39











  • I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

    – Gene100
    Nov 21 '18 at 11:18






  • 1





    The rows of xxxx do give away the attention to detail and style.

    – hrbrmstr
    Nov 21 '18 at 12:54











  • @Gene100: see also this speakerdeck.com/cherdarchuk/…

    – Tung
    Nov 21 '18 at 13:21

















check out this answer in SO. It might be what you're looking for

– Wally Ali
Nov 21 '18 at 4:03





check out this answer in SO. It might be what you're looking for

– Wally Ali
Nov 21 '18 at 4:03




2




2





Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

– atsyplenkov
Nov 21 '18 at 10:39





Also check this article businessinsider.com/pie-charts-are-the-worst-2013-6

– atsyplenkov
Nov 21 '18 at 10:39













I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

– Gene100
Nov 21 '18 at 11:18





I agree, however with a 4 or less pie chart with dissimilar values the pie is more visually appealing to me.

– Gene100
Nov 21 '18 at 11:18




1




1





The rows of xxxx do give away the attention to detail and style.

– hrbrmstr
Nov 21 '18 at 12:54





The rows of xxxx do give away the attention to detail and style.

– hrbrmstr
Nov 21 '18 at 12:54













@Gene100: see also this speakerdeck.com/cherdarchuk/…

– Tung
Nov 21 '18 at 13:21





@Gene100: see also this speakerdeck.com/cherdarchuk/…

– Tung
Nov 21 '18 at 13:21












1 Answer
1






active

oldest

votes


















2














Just change geom_text() to:



geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
position = position_stack(vjust = 0.5))


This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.






share|improve this answer
























  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

    – Gene100
    Nov 21 '18 at 10:45






  • 1





    @Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

    – atsyplenkov
    Nov 21 '18 at 11:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404845%2fadding-labels-to-ggplot2-multi-pie-chart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Just change geom_text() to:



geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
position = position_stack(vjust = 0.5))


This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.






share|improve this answer
























  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

    – Gene100
    Nov 21 '18 at 10:45






  • 1





    @Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

    – atsyplenkov
    Nov 21 '18 at 11:05
















2














Just change geom_text() to:



geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
position = position_stack(vjust = 0.5))


This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.






share|improve this answer
























  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

    – Gene100
    Nov 21 '18 at 10:45






  • 1





    @Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

    – atsyplenkov
    Nov 21 '18 at 11:05














2












2








2







Just change geom_text() to:



geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
position = position_stack(vjust = 0.5))


This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.






share|improve this answer













Just change geom_text() to:



geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))),
position = position_stack(vjust = 0.5))


This will only display labels where your percents are larger than 0%, since those cases don't have a visible area in your plot.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 7:46









alex_555alex_555

6861315




6861315













  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

    – Gene100
    Nov 21 '18 at 10:45






  • 1





    @Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

    – atsyplenkov
    Nov 21 '18 at 11:05



















  • Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

    – Gene100
    Nov 21 '18 at 10:45






  • 1





    @Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

    – atsyplenkov
    Nov 21 '18 at 11:05

















Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

– Gene100
Nov 21 '18 at 10:45





Thanks, works great, except I can't figure out why now an "a" is printed in the center of each of the 3 legend labels located at the bottom. Any idea on how to remove the letter "a" from each of those labels?

– Gene100
Nov 21 '18 at 10:45




1




1





@Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

– atsyplenkov
Nov 21 '18 at 11:05





@Gene100 Set show.legend = FALSE in geom_text: geom_text(aes(label = ifelse(AMOUNT == 0, "", paste0(100*AMOUNT, "%"))), position = position_stack(vjust = 0.5), show.legend = F)

– atsyplenkov
Nov 21 '18 at 11:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404845%2fadding-labels-to-ggplot2-multi-pie-chart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith