饼图内部显示标签,饼图外部显示百分比 [英] Pie chart with label shown inside and percentage shown outside the pie

查看:610
本文介绍了饼图内部显示标签,饼图外部显示百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据,我需要绘制饼图:

I have data like this and I need to draw the pie chart:

# Creat bar chart for jam language
library("ggplot2")  # Data visualization
library("dplyr")    # Data manipulation
library("RColorBrewer")
myPalette <- brewer.pal(5, "Set2") 


all_languages <- data.frame(
  Languages = c("English", "French", "Spanish", "Portuguese", "Others"),
  n = c(81, 4, 3, 2, 3),
  prop = c(87.1, 4.3, 3.2, 2.2, 3.2)
)

# Add label position
all_languages  <-all_languages %>%
  arrange(desc(Languages)) %>%
  mutate(lab.ypos = cumsum(n) - 0.5*n)


ggplot(all_languages , aes(x = "", y = n, fill = Languages)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0)+
  geom_text(aes(y = lab.ypos, label = n), color = "white")+
  scale_fill_manual(values = myPalette) +
  theme_void()

这是我的图表:

现在,我还要在图表外的相应切片处添加百分比.因此,我的英文幻灯片的圆周外将是87.1%.我的百分比存储在all_languages$prop中.我该怎么做?

Now I want to also add the percentage outside the chart at the corresponding slice. So my English slide will have 87.1% outside its circumference. My percentage is stored in all_languages$prop. How do I do this?

第二,如何将标签移离中心一点点?这是因为在较小的切片中,数字标签太杂乱了.

Secondly, how do I move my label just a little bit farther from the center? This is because in the smaller slices, the number labels are too cluttered together.

尝试:我确实尝试遵循此答案

ATTEMPT: I did try to follow this answer How can I put the labels outside of piechart?

但是它抛出一个错误,提示缺少两行...特别是,我的西班牙语和葡萄牙语切片没有标签.另外,我的情况也有所不同,因为我希望既在标签内部又在标签外面.

But it throws an error that says 2 rows are missing... Particularly, my Spanish and Portuguese slices don't have the label. Also, my case is different because I want BOTH label inside and percentage outside.

我尝试使用以下代码遵循该答案:

I tried to follow that answer with the following codes:

all_languages <-all_languages %>%
  arrange(desc(Languages)) %>%
  mutate(end = 2 * pi * cumsum(prop)/sum(prop),
       start = lag(end, default = 0),
       middle = 0.5 * (start + end),
       hjust = ifelse(middle > pi, 1, 0),
       vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))

ggplot(all_languages) + 
  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                   start = start, end = end, fill = Languages)) +
  geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = prop,
                hjust = hjust, vjust = vjust)) +
  coord_fixed() +
  scale_x_continuous(limits = c(-1.5, 1.4),  # Adjust so labels are not cut off
                     name = "", breaks = NULL, labels = NULL) +
  scale_y_continuous(limits = c(-1, 1),      # Adjust so labels are not cut off
                     name = "", breaks = NULL, labels = NULL)

我收到以下警告

警告消息:删除了包含缺失值(geom_text)的2行

Warning message: Removed 2 rows containing missing values (geom_text)

我的馅饼缺少2个切片的标签:

and my pie is missing label for 2 slices:

最终决定:我将按照以下Bappa Das的回答进行操作,并稍微修改标签.我会将计数和百分比都放在饼图之外.

Final decision: I will follow Bappa Das's answer below, and edit the labels a bit. I will put both the count and the percentage outside the pie.

推荐答案

您可以将以下代码与基数R一起使用

You can use the following code with base R

library(RColorBrewer)
Languages = c("English", "French", "Spanish", "Portuguese", "Others")
n = c(81, 4, 3, 2, 3)
prop = c("87.1", "4.3", "3.2", "2.2", "3.2")

lbls <- paste(prop,"%",sep=" ")
lgd <- c("English", "French", "Spanish", "Portuguese", "Others")
cols = brewer.pal(n = length(prop), name = 'Set3')
pie(n, labels = lbls, col=cols)
legend("topright", legend=lgd, cex=0.9, bty = "n", fill = cols)

这是您想要的吗?

这篇关于饼图内部显示标签,饼图外部显示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆