Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

r - Using ggplot2 geom_bar is thare a way to subset two factors on the x axis and group pairs within them?

My dataset contains levels of glottals used in speech of children and their caregivers. Factors are Speaker (a, amum, b, bmum ...), Category (child, caregiver) and Pair (a and amum ...).

My plot shows 22 speakers' levels of glottalisation using the following code:

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

with the following output:

enter image description here

I would like to show bars in two colours (child/caregiver) and have a space between each pair.

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
> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...