【问题标题】:ggplot2 barplot for several categorical variablesggplot2 barplot 用于几个分类变量
【发布时间】:2020-10-14 03:16:52
【问题描述】:

我想创建一个条形图,显示具有相同因子的不同变量。数据集是这样的

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

我想创建一个这样的条形图 barplot

我创建了另一个条形图,显示了我想要的所有信息,但它只有一个变量:

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 with one variable and grouped by Sex 感谢您的帮助

【问题讨论】:

  • 您需要更改数据结构 - 每行一个观察值,每列一个变量。您的新变量将是 ID、软件(级别:Word、Excel、PowerPoint、电子邮件)和熟练程度(级别:未使用、基本、...、高级)。您可以使用 tidyr 包整理您的数据。然后你可以用 x = software 和 fill = proficiency 创建一个 geom_bar。您不需要 position = “dodge” 来获得您在链接中提供的条形图类型。
  • 感谢您的帮助

标签: r ggplot2 bar-chart factors


【解决方案1】:

根据我的评论,您应该执行以下操作来获得所需的堆积条形图。

目前您的数据如下所示:

   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

然后您可以绘制条形图:

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

看起来像这样:

然后,您可以根据需要使用轴标签和构面。

【讨论】:

    【解决方案2】:

    你可以试试这个。希望这能有所帮助。

    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))
    

    【讨论】:

    • 非常感谢您的回答,它完成了工作。这正是我想要的
    猜你喜欢
    • 2019-01-30
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多