在R中的饼图上添加百分比标签

Abd*_*ood 4 charts r ggplot2 pie-chart

我的数据框看起来像

df
   Group   value
1 Positive    52
2 Negative   239
3 Neutral     9
Run Code Online (Sandbox Code Playgroud)

我想使用ggplot制作数据框的饼图.

pie <- ggplot(df, aes(x="", y=value, fill=Group)) +
       geom_bar(width = 1, stat = "identity") +
       coord_polar("y", start=0) 
Run Code Online (Sandbox Code Playgroud)

这是我的饼图.

在此输入图像描述

但是当我尝试在图表上添加百分比标签时

pie <- ggplot(df, aes(x="", y=value, fill=Group)) +
       geom_bar(width = 1, stat = "identity") +
       coord_polar("y", start=0) +
       geom_text(aes(y = value/2 + c(0, cumsum(value)[-length(value)]),
                 label = percent(value/300 )), size=5)
Run Code Online (Sandbox Code Playgroud)

这是我的结果.

在此输入图像描述

我已经看到了许多与我相同的问题,即 R + ggplot2 =>在facet饼图上添加标签,解决方案没有帮助.

hrb*_*str 7

怎么样:

vals <- c(239, 52, 9)
val_names <- sprintf("%s (%s)", c("Negative", "Positive", "Neutral"), scales::percent(round(vals/sum(vals), 2)))
names(vals) <- val_names

waffle::waffle(vals) +
  ggthemes::scale_fill_tableau(name=NULL)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

代替?

它比饼图更"新鲜",你现在并没有真正获得你对这些饼图标签的精确程度.


jaz*_*rro 6

这是匹配饼图中组的顺序和标签顺序的想法。我按降序对数据进行了排序value。我还提前计算了百分比。当我提请ggplot身影,我指定的顺序Group中的顺序mydf(即负,积极,中性)使用fct_inorder()geom_label_repel()为饼图添加标签时,标签的顺序与饼图的顺序相同。

library(dplyr)
library(ggplot2)
library(ggrepel)
library(forcats)
library(scales)

mydf %>%
arrange(desc(value)) %>%
mutate(prop = percent(value / sum(value))) -> mydf 

pie <- ggplot(mydf, aes(x = "", y = value, fill = fct_inorder(Group))) +
       geom_bar(width = 1, stat = "identity") +
       coord_polar("y", start = 0) +
       geom_label_repel(aes(label = prop), size=5, show.legend = F, nudge_x = 1) +
       guides(fill = guide_legend(title = "Group"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

数据

mydf <- structure(list(Group = structure(c(3L, 1L, 2L), .Label = c("Negative", 
"Neutral", "Positive"), class = "factor"), value = c(52L, 239L, 
9L)), .Names = c("Group", "value"), class = "data.frame", row.names = c("1", 
"2", "3"))
Run Code Online (Sandbox Code Playgroud)


Wor*_*ice 5

例如,我创建了一个包含 400 辆车的数据框 e3:

e3 <- data.frame(400)
e3 <- rep( c("car", "truck", "other", "bike", "suv"), c(60, 120, 20, 50, 150))
Run Code Online (Sandbox Code Playgroud)

由于饼图对于比例特别有用,让我们来看看我们车辆的比例,然后我们将在这种情况下报告图表:

paste(prop.table(table(e3))*100, "%", sep = "")
[1] "15%"   "5%"    "30%"   "12.5%" "37.5%"
Run Code Online (Sandbox Code Playgroud)

然后你就可以画饼图了

pie(table(e3), labels = paste(round(prop.table(table(e3))*100), "%", sep = ""), 
col = heat.colors(5), main = "Vehicles proportions - n: 400")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Pet*_*lis 2

我同意@hrbrmstr 的观点,华夫饼图会更好。但要回答原来的问题......你的问题来自于楔形的绘制顺序,默认为字母顺序。当您根据数据框中的顺序计算标签的放置位置时,结果是错误的。

作为可读性的一般原则,在绘制图形的实际代码之前对标签和它们所在的位置进行所有奇特的计算。

library(dplyr)
library(ggplot2)
library(ggmap) # for theme_nothing
df <- data.frame(value = c(52, 239, 9),
                 Group = c("Positive", "Negative", "Neutral")) %>%
   # factor levels need to be the opposite order of the cumulative sum of the values
   mutate(Group = factor(Group, levels = c("Neutral", "Negative", "Positive")),
          cumulative = cumsum(value),
          midpoint = cumulative - value / 2,
          label = paste0(Group, " ", round(value / sum(value) * 100, 1), "%"))

ggplot(df, aes(x = 1, weight = value, fill = Group)) +
   geom_bar(width = 1, position = "stack") +
   coord_polar(theta = "y") +
   geom_text(aes(x = 1.3, y = midpoint, label = label)) +
   theme_nothing()               
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述