带有特定顺序和百分比注释的ggplot2饼图 [英] pie chart with ggplot2 with specific order and percentage annotations

查看:166
本文介绍了带有特定顺序和百分比注释的ggplot2饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框:

  + -------- + ------ ----- + ----- + 
| make |模型| cnt |
+ -------- + ----------- + ----- +
|丰田| camry | 10 |
|丰田|花冠| 4 |
|本田|城市| 8 |
|本田|协议| 13 |
|吉普车指南针| 3 |
|吉普车牧师| 5 |
|吉普车叛徒| 1 |
| accura | x1 | 2 |
| accura | x3 | 1 |
+ -------- + ----------- + ----- +

我需要为每个产品创建一个饼图(是的真正的)百分比份额。





  library(ggplot2)
library(dplyr)

df < - data.frame(Make = c('toyota','toyota','honda','honda','jeep','jeep','jeep','accura','accura'),
Model = c('camry','corolla','city','accord','compass','wrangler','renegade','x1','x3'),
Cnt = c(10, ($)
dfc < - df%>%
group_by(品牌)%>%
汇总(量= sum(Cnt))%>%
mutate(share = volume / sum(volume)* 100.0)%>%
arrange(desc(volume))

bp < - ggplot(dfc [c(1:10),],aes(x =,y = share,fill = Make))+
geom_bar(width = 1,stat =identity)
pie < - bp + coord_polar(y)
pie

给我下面的饼图非常整洁。



然而,我需要使用如下图所示。


  1. 添加百分比标签

  2. 订单共享

  3. 删除类似0/100,25的标签

  4. 添加标题

解决方案

您必须将制作的级别改为 share volume (提供的数据已经排序):

  dfc $ Make < -  factor(dfc $ make,levels = rev(as.character(dfc $ Make)))

code> theme arg

  ggplot(dfc [1:10,],aes(,share,fill = Make))+ 
geom_bar(width = 1,size = 1,color =white,stat =identity)+
coord_polar(y)+
geom_text(aes(label = paste0 (share),%)),
position = position_stack(vjust = 0.5))+
labs(x = NULL,y = NULL,fill = NULL,
title =market分享)+
指南(fill = guide_legend(reverse = TRUE))+
scale_fill_manual(values = c(#ffd700,#bcbcbc,#ffa500#254290) )+
theme_classic()+
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5,color =#666666))

< a href =https://i.stack.imgur.com/gS8DV.png =nofollow noreferrer>


I have a data frame like below

+--------+-----------+-----+
|  make  |   model   | cnt |
+--------+-----------+-----+
| toyota |  camry    |  10 |
| toyota |  corolla  |   4 |
| honda  |  city     |   8 |
| honda  |  accord   |  13 |
| jeep   |  compass  |   3 |
| jeep   |  wrangler |   5 |
| jeep   |  renegade |   1 |
| accura |  x1       |   2 |
| accura |  x3       |   1 |
+--------+-----------+-----+

I need to create a pie ( yes really) of the percentage share for each make.

I do the following as of now.

library(ggplot2)
library(dplyr)

df <- data.frame(Make=c('toyota','toyota','honda','honda','jeep','jeep','jeep','accura','accura'),
                 Model=c('camry','corolla','city','accord','compass', 'wrangler','renegade','x1', 'x3'),
                 Cnt=c(10, 4, 8, 13, 3, 5, 1, 2, 1))
dfc <- df %>%
  group_by(Make) %>%
  summarise(volume = sum(Cnt)) %>%
  mutate(share=volume/sum(volume)*100.0) %>%
  arrange(desc(volume))

bp <- ggplot(dfc[c(1:10),], aes(x="", y= share, fill=Make)) +
  geom_bar(width = 1, stat = "identity")
pie <- bp + coord_polar("y")
pie

This gives me the following pie chart which is pretty neat.

However I need to enhance this with the following things - like in the image below.

  1. add percentage labels
  2. order the pies in desc order of share
  3. remove lables like 0/100, 25
  4. add a title

解决方案

You have to change levels of Make by share or volume (provided data is already sorted):

dfc$Make <- factor(dfc$Make, levels = rev(as.character(dfc$Make)))

And play with theme arguments:

ggplot(dfc[1:10, ], aes("", share, fill = Make)) +
    geom_bar(width = 1, size = 1, color = "white", stat = "identity") +
    coord_polar("y") +
    geom_text(aes(label = paste0(round(share), "%")), 
              position = position_stack(vjust = 0.5)) +
    labs(x = NULL, y = NULL, fill = NULL, 
         title = "market share") +
    guides(fill = guide_legend(reverse = TRUE)) +
    scale_fill_manual(values = c("#ffd700", "#bcbcbc", "#ffa500", "#254290")) +
    theme_classic() +
    theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          plot.title = element_text(hjust = 0.5, color = "#666666"))

这篇关于带有特定顺序和百分比注释的ggplot2饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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