簡體   English   中英

幾個分類變量的 ggplot2 條形圖

[英]ggplot2 barplot for several categorical variables

我想創建一個條形圖,顯示具有相同因子的不同變量。 數據集是這樣的

ID;Word;Excel;Power Point;Correo electronico
11;Intermedio;Intermedio;Intermedio;Intermedio
25;Intermedio;Intermedio;Intermedio;Intermedio
26;Básico;No la he utilizado;Básico;Básico
27;Básico;Básico;Básico;Intermedio
29;Intermedio;Intermedio;Intermedio;Avanzado
33;Avanzado;Intermedio;Avanzado;Avanzado
37;Avanzado;Básico;Intermedio;Avanzado
39;Intermedio;Intermedio;No la he utilizado;Intermedio
43;Intermedio;Intermedio;Intermedio;Intermedio
51;Avanzado;Básico;Intermedio;Avanzado
53;Intermedio;Intermedio;Intermedio;Intermedio
54;Intermedio;Intermedio;Básico;Avanzado
60;Intermedio;Intermedio;Intermedio;Intermedio

我想創建一個像這樣的條形圖

我創建了另一個條形圖,它顯示了我想要的所有信息,但它只有一個變量:

ggplot(aes(x = Tablet, 
             y = prop.table(stat(count)), 
             fill = factor(Tablet), 
             label = scales::percent(prop.table(stat(count)), accuracy = 0.01))) +
  scale_fill_manual(values = CColors)+
  
  geom_bar(position = "dodge")  + 
  geom_text( aes(label = scales::percent((..count..)/sum(..count..), accuracy = 0.01),
                 y= ((..count..)/sum(..count..))), stat="count",
             vjust = -0.5,
             size = 3, 
             hjust=1)+
  scale_y_continuous(labels = scales::percent) + 
  labs(y = 'Frecuencia relativa', fill = 'Uso de Tablet')+
  facet_wrap( ~ Sexo, nrow = 1)+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+ ggtitle(paste("Uso de Tablet para acceder a clases virtuales \nTotal: ", nrow(das), " Encuestados"))

結果是這個帶有一個變量並按性別分組的 Barplot感謝您的幫助

你可以試試這個。 希望這能有所幫助。

library(reshape2)
library(ggplot2)
#Load your data
Data <- structure(list(ID = c(11L, 25L, 26L, 27L, 29L, 33L, 37L, 39L, 
43L, 51L, 53L, 54L, 60L), Word = structure(c(3L, 3L, 2L, 2L, 
3L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 3L), .Label = c("Avanzado", "Básico", 
"Intermedio"), class = "factor"), Excel = structure(c(2L, 2L, 
3L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L), .Label = c("Básico", 
"Intermedio", "No la he utilizado"), class = "factor"), Power.Point = structure(c(3L, 
3L, 2L, 2L, 3L, 1L, 3L, 4L, 3L, 3L, 3L, 2L, 3L), .Label = c("Avanzado", 
"Básico", "Intermedio", "No la he utilizado"), class = "factor"), 
    Correo.electronico = structure(c(3L, 3L, 2L, 3L, 1L, 1L, 
    1L, 3L, 3L, 1L, 3L, 1L, 3L), .Label = c("Avanzado", "Básico", 
    "Intermedio"), class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))
#Melt data
Data.Melt <- melt(Data,id.vars = 'ID')
Data.Melt %>% group_by(variable,value) %>% summarise(N=n()) -> Dat1
#Plot
ggplot(Dat1,aes(x=variable,y=N,fill=value,label=N))+
  geom_bar(position="stack", stat="identity")+
  geom_text(size = 3, position = position_stack(vjust = 0.5))

在此處輸入圖像描述

根據我的評論,您應該采取以下措施來獲得您想要的堆疊條 plot。

目前您的數據如下所示:

   ID       Word              Excel         PowerPoint Correo.electronico
2  11 Intermedio         Intermedio         Intermedio         Intermedio
3  25 Intermedio         Intermedio         Intermedio         Intermedio
4  26     Básico No la he utilizado             Básico             Básico
5  27     Básico             Básico             Básico         Intermedio
6  29 Intermedio         Intermedio         Intermedio           Avanzado
7  33   Avanzado         Intermedio           Avanzado           Avanzado
8  37   Avanzado             Básico         Intermedio           Avanzado
9  39 Intermedio         Intermedio No la he utilizado         Intermedio
10 43 Intermedio         Intermedio         Intermedio         Intermedio
11 51   Avanzado             Básico         Intermedio           Avanzado
12 53 Intermedio         Intermedio         Intermedio         Intermedio
13 54 Intermedio         Intermedio             Básico           Avanzado
14 60 Intermedio         Intermedio         Intermedio         Intermedio

您可以將其整理為每行一個觀察值,每列一個變量:

library(tidyr)
df_tidy <- df %>% pivot_longer(-ID, names_to = "software")
df_tidy

# A tibble: 52 x 3
   ID    software           value             
   <chr> <chr>              <chr>             
 1 11    Word               Intermedio        
 2 11    Excel              Intermedio        
 3 11    PowerPoint         Intermedio        
 4 11    Correo.electronico Intermedio        
 5 25    Word               Intermedio        
 6 25    Excel              Intermedio        
 7 25    PowerPoint         Intermedio        
 8 25    Correo.electronico Intermedio        
 9 26    Word               Básico            
10 26    Excel              No la he utilizado
# … with 42 more rows

然后你可以 plot 你的酒吧 plot:

library(ggplot2)
ggplot(df_tidy, aes(x = software, fill = value)) +
        geom_bar()

它看起來像這樣:

在此處輸入圖像描述

然后,您可以根據需要使用軸標簽和構面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM