【问题标题】:Using ggplot2 geom_bar is thare a way to subset two factors on the x axis and group pairs within them?使用 ggplot2 geom_bar 是一种在 x 轴上对两个因子进行子集化并在其中分组对的方法?
【发布时间】:2021-04-17 10:49:11
【问题描述】:

我的数据集包含儿童及其看护人讲话中使用的声门级别。因子是Speaker (a, amum, b, bmum ...), Category (child, caregiver) 和Pair (a and amum ...)。

我的情节使用以下代码显示了 22 位说话者的声门化水平:

ggplot(data1, aes(x = Speaker, fill=Glottals, colour = Category)) + 
  geom_bar(position ="fill") + scale_fill_manual(values = c("White", "grey")) + 
  theme_minimal() + ggtitle ("Percent glottals for caregiver pairs") + ylab("Percent") + scale_y_continuous(labels = scales::percent) + xlab("Speaker") + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + theme(legend.position = "none")

输出如下:

我想以两种颜色显示条形(儿童/看护人),并在每对之间留一个空格。

A tibble: 3,303 x 15
   Context Transcript   Pair  Speaker Ch_Gender   Age Category    ID Gender Before               Text   After   Phon         Glottals Sitcon 
   <fct>   <fct>        <fct> <fct>   <fct>     <dbl> <fct>    <int> <fct>  <fct>                <fct>  <fct>   <fct>        <fct>    <fct>  
 1 Home    David_H1.trs David David   M           3.2 Child        3 M      "It's not* coming"   out    ""      coda pause   t        routine
 2 Home    David_H1.trs David David   M           3.2 Child        3 M      "I'll (inc)"         that   "out."  coda vowel   ?        routine
 3 Home    David_H1.trs David David   M           3.2 Child        3 M      "I'll (inc) that"    out.   ""      coda pause   ?        routine
 4 Home    David_H1.trs David David   M           3.2 Child        3 M      ""                   Butter ""      Intervocalic t        routine
 5 Home    David_H1.trs David David   M           3.2 Child        3 M      ""                   Butter "Flour" Intervocalic t        routine
 6 Home    David_H1.trs David David   M           3.2 Child        3 M      "Mummy there's the"  butter ""      Intervocalic t        routine
 7 Home    David_H1.trs David David   M           3.2 Child        3 M      "Mummy can you open" that?  ""      coda pause   ?        routine
 8 Home    David_H1.trs David David   M           3.2 Child        3 M      "Mummy I"            want-- ""      coda pause   ?        routine
 9 Home    David_H1.trs David David   M           3.2 Child        3 M      "I want to do the"   butter ""      Intervocalic t        routine
10 Home    David_H1.trs David David   M           3.2 Child        3 M      "I wanna do the"     fruit  ""      coda pause   ?        routine
# ... with 3,293 more rows
> 

【问题讨论】:

    标签: r ggplot2 grouping subset factors


    【解决方案1】:

    试试这个:

    #Code
    ggplot(data1, aes(x = Speaker, fill=Glottals, fill = Category)) + 
    geom_bar(position ="fill") + scale_fill_manual(values = c("White", "grey")) + theme_minimal() + ggtitle ("Percent glottals for caregiver pairs") + ylab("Percent") + scale_y_continuous(labels = scales::percent) + xlab("Speaker") + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + theme(legend.position = "none")
    

    【讨论】:

    • 非常感谢代码。是否可以有两种颜色(儿童和看护人),并且每对都与下一个间隔开。
    • @Marj 我需要查看您的数据。您能否使用dput(data1) 并粘贴输出编辑您的问题?
    • 我是新手。如何发送我的数据集?
    • 您可以使用dput(data1) 并复制结果,然后单击问题下方的编辑。粘贴并保存更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多