Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping by two variables in ggplot2

Tags:

r

ggplot2

enter image description hereI'm trying to create a bar graph using ggplot2 to show the occurrence of different consonants in some language data. For most subjects there is only one timepoint sampled, but for some of the subjects there are two timepoints. The subjects are grouped into two different groups for comparison, and their consonant production is grouped by type - a phonetic variable in my data.

I want to show the consonants produced by each subject in a stacked bar, and then have data from the speakers who were recorded twice show next to each other on the x axis. At the moment, ggplot is aggregating this data. I don't want to facet the data into two different plots.

 exampledata <- tribble(~subject, ~group, ~time, ~consonant, ~type,  ~n,
     "1", "A", 1, "p", F, 10,
     "1", "A", 1, "t", T, 12,
     "1", "A", 1, "k", T, 50,
     "2", "A", 1, "p", T, 0,
     "2", "A", 1, "t", T, 45,
     "2", "A", 1, "k", F, 23,
     "2", "A", 2, "p", F, 2,
     "2", "A", 2, "t", T, 34,
     "2", "A", 2, "k", T, 56,
     "3", "B", 1, "p", F, 12,
     "3", "B", 1, "t", T, 13,
     "3", "B", 1, "k", F, 50,
     "4", "A", 1, "p", T, 10,
     "4", "A", 1, "t", F, 12,
     "4", "A", 1, "k", T, 50,
     "5", "B", 1, "p", T, 0,
     "5", "B", 1, "t", T, 24,
     "5", "B", 1, "k", F, 3,
     "5", "B", 2, "p", F, 23,
     "5", "B", 2, "t", F, 12,
     "5", "B", 2, "k", T, 7,
     "6", "A", 1, "p", F, 52,
     "6", "A", 1, "t", F, 12,
     "6", "A", 1, "k", T, 64
     )

I'm currently using the following code, which generates the attached figure:

 plot1 <- ggplot(data=exampledata, aes(x=subject, y=n, fill=type, colour = group)) +
   geom_bar(stat="identity") +
   scale_fill_manual(values=c("gray97", "gray87")) +
   scale_colour_manual(values = c("royalblue", "navyblue")) +
   theme_bw()
 plot(plot1)
 [![example bar plot][1]][1]

So all I want to do now is somehow create an extra grouping variable that shows time bars side by side.

like image 647
Catherine Laing Avatar asked Oct 30 '25 20:10

Catherine Laing


2 Answers

You can try to work with a facet but removing the faced-like shape

ggplot(exampledata, aes(x= time, y=n, fill=type))  +  
  geom_col() +
  facet_grid(~subject, scales = "free_x", switch = "x") + 
  xlab("subject") + 
  theme_bw() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.border =element_blank(),
        strip.background = element_blank())

enter image description here

like image 121
Roman Avatar answered Nov 01 '25 09:11

Roman


Is that what you want?

plot1 <- ggplot(data=exampledata, aes(x=subject, y=n, fill=type, colour = group, group = time)) +
  geom_bar(stat="identity", position = 'dodge') +
  scale_fill_manual(values=c("gray97", "gray87")) +
  scale_colour_manual(values = c("royalblue", "navyblue")) +
  theme_bw()[![enter image description here][1]][1]

enter image description here

like image 38
JAQuent Avatar answered Nov 01 '25 11:11

JAQuent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!