Диаграмма P ie с ggplot2, считая вхождения записей - PullRequest
1 голос
/ 27 января 2020

Я хотел бы иметь возможность создать «диаграмму P ie» в R с помощью ggplot2, но с учетом случаев, когда появляются определенные данные.

В моем примере у меня есть Excel, из которого я Извлеките столбец, и значения, которые появляются в столбцах, например, столбец с именем «дисциплина», имеет значения:

discipline1, discipline2, discipline3, discipline1, discipline1, discipline2,
discipline2, discipline2, discipline2, discipline2, discipline3, discipline3. 

Итак, я хочу нарисовать процент появления каждого из значений в%:

discipline1, discipline2, discipline3

с использованием диаграммы P ie.

Для гистограммы я использовал значение stat="count", но я видел, что на диаграмме P ie я могу ' т.

Например, я пробовал что-то вроде этого:

ggplot(df, aes(x = "", y = as.factor(df[,discipline]), 
               fill = as.factor(df[,discipline]))) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0)+
  geom_text(aes(y = as.factor(df[,discipline]), 
                label = as.factor(df[,discipline])), color = "white")+
  scale_fill_manual(values =c("#0073C2FF", "#EFC000FF", "#868686FF"))+
  theme_void()

Ответы [ 3 ]

1 голос
/ 27 января 2020

Вы можете просто добавить coord_polar(theta='y') после вашего geom_bar:

library(ggplot2)

ggplot(data=as.data.frame(discipline), 
        aes(x = factor(1),fill = factor(discipline))) + 
  geom_bar(stat = "count") + 
  scale_y_continuous(breaks = seq(0,12,3), labels = c("0", "25%", "50%", "75%", "100%")) + 
  coord_polar(theta='y') +
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank()) +
  labs(fill = "Discipline")

Обновление:

Если вы хочу добавить метки с geom_text, я предлагаю другой подход вместо stat = 'count'; см. ниже:

library(dplyr)
library(ggplot2)
library(scales)

data.frame(discipline) %>% 
  group_by(discipline) %>% 
  mutate(cnt = n(),
         pct = percent(cnt / nrow(.), accuracy = 1)) %>%
  unique %>% ungroup %>% 
  mutate(place = cumsum(cnt) - cnt/2) %>% 
ggplot(data = ., 
         aes(x = factor(1), weight = cnt, fill = factor(discipline))) + 
  geom_bar(position = "stack") + 
  scale_y_continuous(breaks = seq(0, length(discipline), length(discipline)/4), 
                     labels = c("0", "25%", "50%", "75%", "100%")) + 
  coord_polar(theta='y') +
  geom_text(aes(x = 1.1, y = place, label = pct)) +
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank()) +
  labs(fill = "Discipline")

Данные:

discipline <- c("discipline1", "discipline2", "discipline3", "discipline1", 
                "discipline1", "discipline2", "discipline2", "discipline2", 
                "discipline2", "discipline2", "discipline3", "discipline3")
0 голосов
/ 27 января 2020

Первый комментарий не работает для меня

я написал:

ggplot(df,aes(x= factor(1), fill= factor(df[,discipline])))+
  geom_bar(stat="count")+ 
 scale_y_continuous(breaks = seq(0,12,3), labels = c("0", "25%", "50%", "75%", "100%")) + 
  coord_polar(theta='y') +
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank()) +
  labs(fill = "Discipline")

И я получил:

Pie Chart

где "df" - это кадр данных, полученный при чтении csv, а коэффициент (df [, дисциплина]):

  [1] Firmware       Container      Management    
  [4] Management     IT             Management    
  [7] Management     Firmware       Firmware      
 [10] Factory        Factory        Factory       
 [13] Factory        Factory        Factory       
 [16] Management     Management     Management    
 [19] Electrical     Electrical     Electrical    
 [22] Factory        Factory        Electrical    
 [25] Container      Mechanical     Electrical    
 [28] Assurance      Management     Management    
 [31] Mechanical     Mechanical     Mechanical    
 [34] Mechanical     Mechanical     Mechanical    
 [37] Lab            Electrical     Electrical    
 [40] Electrical     Assurance      Container     
 [43] Electrical     Assurance      Management    
 [46] Management     Mechanical     Mechanical    
 [49] Factory        Mechanical     Mechanical    
 [52] Mechanical     Mechanical     Mechanical    
 [55] Mechanical     Mechanical     Mechanical    
 [58] Mechanical     Mechanical     Electrical    
 [61] Electrical     Electrical     Factory       
 [64] Factory        Electrical     Electrical    
 [67] Lab            Electrical     Electrical    
 [70] Electrical     Management     Electrical    
 [73] Assurance      Firmware       Firmware      
 [76] Electrical     Electrical     Electrical    
 [79] Electrical     Electrical     Electrical    
 [82] Electrical     Electrical     Electrical    
 [85] Electrical     Electrical     Electrical    
 [88] Electrical     Electrical     Electrical    
 [91] Electrical     Electrical     Electrical    
 [94] Electrical     Factory        Factory       
 [97] Electrical     Electrical     Electrical    
[100] Electrical     Electrical     Electrical    
[103] Electrical     Electrical     Electrical    
[106] Electrical     Electrical     Electrical    
[109] Electrical     Electrical     Electrical    
[112] Electrical     Electrical     Electrical    
[115] Electrical     Electrical     Factory       
[118] Electrical     Electrical     Electrical    
[121] Electrical     Electrical     Electrical    
[124] Electrical     Management     Management    
[127] Firmware       Electrical     Management    
[130] Firmware       Electrical                   
[133]                Software       Assurance     
[136] Assurance      Assurance      Firmware      
[139] Firmware       Assurance      Assurance     
[142] Firmware       Firmware       Firmware      
[145] Firmware       Container      Container     
[148] Firmware       Firmware       Firmware      
[151] Firmware       Firmware       Firmware      
[154] Firmware       Firmware       Firmware      
[157] Firmware       Firmware       Firmware      
[160] Firmware       Firmware       Firmware      
[163] Firmware       Firmware       Firmware      
[166] Firmware       Firmware       Firmware      
[169] Firmware       Firmware       Firmware      
[172] Firmware       Firmware       Firmware      
[175] Firmware       Firmware       Firmware      
[178] Firmware       IT             Management    
[181] Management     Software                     
[184]                                             
[187]                                             
[190]                               Mechanical    
[193] Assurance      Assurance      Assurance     
[196] Assurance      Assurance      Assurance     
[199] Assurance                     Assurance     
[202] Assurance      Assurance      Assurance     
[205] Assurance      Assurance      Assurance     
[208] Assurance      Assurance      Assurance     
[211] Assurance      Assurance      Assurance     
[214] Assurance      Assurance      Assurance     
[217] Assurance      Assurance      Assurance     
[220] Assurance      Assurance                    
[223] IT             IT             IT            
[226] Management     Management     Management    
[229] Mechanical     Management     Management    
[232] Mechanical     Electrical                   
[235] Mechanical     Mechanical     Mechanical    
[238] Mechanical     Mechanical                   
[241]                               Management    
[244] Management                    Management    
[247] Electrical     Electrical     Electrical    
[250] Mechanical     Mechanical     Management    
[253] Container      Electrical     Container     
[256] Container      Container      Container     
[259] Container      Container      Container     
[262] Electrical     Electrical     Electrical    
[265] Electrical     Electrical     Electrical    
[268] Electrical     Firmware       Firmware      
[271] Firmware       Firmware       Firmware      
[274] Firmware       Mechanical     Mechanical    
[277] Container      Assurance      Assurance     
[280] Assurance      Assurance      Assurance     
[283] Container                     Management    
[286] Container      Container      Container     
[289] Container      Container      Management    
[292] Management     Electrical     Firmware      
[295] Factory        Factory        Mechanical    
[298] Management     Management                   
[301] Electrical     Electrical     Electrical    
[304] Electrical     Electrical     Electrical    
[307]                Management     Management    
[310] Management     Management     Management    
[313] Management     Management     Assurance     
[316] Assurance      Assurance      Research      
[319] Research       Management     Management    
[322] Firmware       Electrical     Management    
[325] Firmware       Electrical     Management    
[328] Management     Firmware       Electrical    
[331] Mechanical     Management     Management    
[334] Management     Management     Management    
[337] Mechanical     Firmware       Management    
[340] Mechanical     Firmware       Electrical    
[343] Management     Management     Management    
[346] Mechanical     Electrical     Electrical    
[349] Management     Mechanical     Firmware      
[352] Electrical     Management                   
[355] Mechanical     Electrical     Management    
[358] Firmware                                    
[361] Mechanical     Mechanical     Mechanical    
[364] Mechanical     Mechanical     Container     
[367] Management     Container      Container     
[370] Management     Electrical     Electrical    
[373] Electrical     Electrical     Electrical    
[376] Electrical     Electrical     Electrical    
[379] Electrical     Electrical     Firmware      
[382] Firmware       Firmware       Firmware      
[385] Firmware       Management     Mechanical    
[388] Mechanical     Mechanical     Management    
[391]                Assurance      Assurance     
[394] Assurance      Assurance      Assurance     
[397] Assurance      Firmware       Electrical    
[400] Quality                       Quality       
[403] Quality        Quality        Quality       
[406] Quality        Firmware       Firmware      
[409] Assurance      Factory                      
[412] Software       IT             Lab           
[415]                IT                           
[418] IT                                          
[421]                IT                           
[424]                Assurance      Assurance     
[427] Firmware       Firmware                     
[430] Software       Quality        Electrical    
[433]                                             
[436]                                             
[439] Assurance      Firmware       Firmware      
[442] Mechanical     Mechanical     SCM           
[445]                IT             Management    
[448] Management     Management     Management    
[451] Factory        Assurance      IT            
[454]                                             
[457]                Firmware       Assurance     
[460] Assurance      Firmware       Assurance     
[463] Mechanical                    Mechanical    
[466] Mechanical     IT             IT            
[469] Assurance      Assurance      Assurance     
[472] Mechanical                    Assurance     
[475]                                             
[478] Electrical                    Mechanical    
[481] Mechanical     Mechanical     Mechanical    
[484]                Mechanical     Firmware      
[487] Assurance      Assurance      Assurance     
[490] Firmware                      Mechanical    
[493] Firmware       Assurance      Management    
[496] Management     Management     Management    
[499] Management     Assurance      Management    
[502] Quality        Assurance      Management    
[505] Assurance      Management     Management    
[508] Management                                  
[511]                Electrical     Firmware      
[514] Mechanical     Assurance      Electrical    
[517] Electrical     Electrical     Electrical    
[520] Electrical     Electrical     Electrical    
[523] Electrical     Electrical     Electrical    
[526] Electrical     Electrical     Electrical    
[529] Electrical     Electrical     Electrical    
[532] Electrical     Firmware       Firmware      
[535] Firmware       Factory        Firmware      
[538] Firmware       Firmware       Firmware      
[541] Firmware       Firmware       Firmware      
[544] Firmware       Firmware       Firmware      
[547] Firmware       Firmware       Firmware      
[550] Mechanical     Mechanical     Mechanical    
[553] Mechanical     Mechanical     Mechanical    
[556] Mechanical     Mechanical     Assurance     
[559] Assurance      Assurance      Assurance     
[562] Assurance      Assurance      Assurance     
[565] Assurance      Assurance      Assurance     
[568] Assurance      Management     Assurance     
[571] Assurance      Assurance      Factory       
[574] IT             IT             IT            
[577] IT             IT             Lab           
[580] Management     Electrical     Electrical    
[583]                Mechanical     Firmware      
[586] Administration Mechanical     Electrical    
[589] Firmware       Mechanical     AI            
[592] Lab           
16 Levels:  Administration AI ... Software

Не могли бы вы сказать, что не так с кодом? Еще раз спасибо.

0 голосов
/ 27 января 2020

Это отвечает на ваш вопрос?

library(tidyverse)

# sample data
df <- tibble(discipline = paste0("discipline", sample(1:3, 20, replace = T))) %>%
  count(discipline)

# creating a pie chart
ggplot(df, aes(x="", y= n , fill=discipline))+
  geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...