Hi All, I have df like this:> df# A tibble: 2 ? 4V1 n perc labels <chr> <int> <dbl> <chr> 1 Yes 8 0.364 36% 2 No 14 0.636 64% I am making pie chart like this: library(ggplot2) ggplot(df, aes(x = "", y = perc, fill = V1)) + geom_col(color = "black") + geom_label(aes(label = labels), position = position_stack(vjust = 0.5), show.legend = FALSE) + guides(fill = guide_legend(title = "Answer")) + coord_polar(theta = "y") + theme_void() How would I add in the legend beside Answer "Yes" count 8 (just number 8) and beside "No" count 14? Thanks Ana [[alternative HTML version deleted]]
Christopher W Ryan
2022-Aug-15 16:06 UTC
[R] [External Email] how to add count to pie chart legend
Perhaps just use a table, and skip the pie chart? --Chris Ryan On Mon, Aug 15, 2022 at 11:59 AM Ana Marija <sokovic.anamarija at gmail.com> wrote:> Hi All, > > I have df like this: > > > df# A tibble: 2 ? 4 > V1 n perc labels > <chr> <int> <dbl> <chr> 1 Yes 8 0.364 36% 2 No 14 0.636 64% > > I am making pie chart like this: > > library(ggplot2) > > ggplot(df, aes(x = "", y = perc, fill = V1)) + > geom_col(color = "black") + > geom_label(aes(label = labels), > position = position_stack(vjust = 0.5), > show.legend = FALSE) + > guides(fill = guide_legend(title = "Answer")) + > coord_polar(theta = "y") + > theme_void() > > How would I add in the legend beside Answer "Yes" count 8 (just number > 8) and beside "No" count 14? > > Thanks > > Ana > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
V1=c("Yes","No") n=c(8,14) perc=c(0.364,0.636) labels=c("35%","64%") df=data.frame(V1,n,perc,labels) library(ggplot2) ggplot(df, aes(x = "", y = perc, fill = V1)) + geom_col(color = "black") + geom_label(aes(label = labels), position = position_stack(vjust = 0.5), show.legend = FALSE) + guides(fill = guide_legend(title = "Answer")) + coord_polar(theta = "y") + theme_void() + geom_text(aes(label=n), position=position_stack(vjust=0.25)) library(ggplot2) ggplot(df, aes(x = "", y = perc, fill = V1)) + geom_col(color = "black") + geom_label(aes(label = labels), position = position_stack(vjust = 0.5), show.legend = FALSE) + guides(fill = guide_legend(title = "Answer")) + coord_polar(theta = "y") + theme_void() + geom_text(aes(label=c("n=8","n=14")), position=position_stack(vjust=0.25)) There are two variants of geom_text. They have slightly different output. Play with vjust to get the location right. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Ana Marija Sent: Monday, August 15, 2022 11:52 AM To: r-help <r-help at r-project.org> Subject: [R] how to add count to pie chart legend [External Email] Hi All, I have df like this:> df# A tibble: 2 ? 4V1 n perc labels <chr> <int> <dbl> <chr> 1 Yes 8 0.364 36% 2 No 14 0.636 64% I am making pie chart like this: library(ggplot2) ggplot(df, aes(x = "", y = perc, fill = V1)) + geom_col(color = "black") + geom_label(aes(label = labels), position = position_stack(vjust = 0.5), show.legend = FALSE) + guides(fill = guide_legend(title = "Answer")) + coord_polar(theta = "y") + theme_void() How would I add in the legend beside Answer "Yes" count 8 (just number 8) and beside "No" count 14? Thanks Ana [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C63484df8e9fa437560ec08da7ed72970%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637961759829882759%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=K%2BcWZLcFC5eVA5f5nogJvxLVzYadQ1sVgkdJPBVaaGo%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C63484df8e9fa437560ec08da7ed72970%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637961759829882759%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=6qDQiSaEy1WUChkgKjJdV8S%2FnH0bYOv2qloNGzpGt9E%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
Hi Ana, A lot of work for a little pie. df<-read.table(text="V1 n Yes 8 No 14", header=TRUE, stringsAsFactors=FALSE) par(mar=c(5,4,4,4)) pie(df$n,df$V1,col=c(3,2),main="Yes and No", xlab="",ylab="",radius=1) legend(0.75,-0.8,paste(df$V1,df$n),fill=c(3,2), xpd=TRUE) Jim On Tue, Aug 16, 2022 at 1:59 AM Ana Marija <sokovic.anamarija at gmail.com> wrote:> > Hi All, > > I have df like this: > > > df# A tibble: 2 ? 4 > V1 n perc labels > <chr> <int> <dbl> <chr> 1 Yes 8 0.364 36% 2 No 14 0.636 64% > > I am making pie chart like this: > > library(ggplot2) > > ggplot(df, aes(x = "", y = perc, fill = V1)) + > geom_col(color = "black") + > geom_label(aes(label = labels), > position = position_stack(vjust = 0.5), > show.legend = FALSE) + > guides(fill = guide_legend(title = "Answer")) + > coord_polar(theta = "y") + > theme_void() > > How would I add in the legend beside Answer "Yes" count 8 (just number > 8) and beside "No" count 14? > > Thanks > > Ana > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.