在ggplot2中按两个变量分组

时间:2019-07-23 10:27:57

标签: r ggplot2

enter image description here我正在尝试使用ggplot2创建条形图,以显示某些语言数据中不同辅音的出现。对于大多数主题,只有一个时间点被采样,但是对于某些主题,只有两个时间点。这些对象被分为两个不同的组进行比较,其辅音根据type分组-我数据中的语音变量。

我想在堆叠的条形图中显示每个对象产生的辅音,然后将两次录制的扬声器的数据在x轴上彼此相邻显示。目前,ggplot正在汇总此数据。我不想将数据分成两个不同的图。

 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
     )

我当前正在使用以下代码,该代码生成附图:

 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]

所以我现在要做的就是以某种方式创建一个额外的分组变量,以并排显示time条。

4 个答案:

答案 0 :(得分:2)

在@JAQuent的响应中,尽管很优雅,但不保留堆栈。 我建议这样做:

exampledata$interaction <- factor(interaction(exampledata$subject, exampledata$time),
                                  levels = sprintf("%d.%d", sort(rep(1:6, 2)), rep(1:2, 6)))

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

enter image description here

答案 1 :(得分:1)

这就是你想要的吗?

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

答案 2 :(得分:1)

这有点骇人听闻,但这确实可以满足您的要求。

首先,对于主题,时间,组和类型的所有组合,您的数据都必须包含零。像这样:

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,
                       "1", "A", 2, "p", F, 0,
                       "1", "A", 2, "t", T, 0,
                       "1", "A", 2, "k", T, 0,
                       "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,
                       "3", "B", 2, "p", F, 0,
                       "3", "B", 2, "t", T, 0,
                       "3", "B", 2, "k", F, 0,
                       "4", "A", 1, "p", T, 10,
                       "4", "A", 1, "t", F, 12,
                       "4", "A", 1, "k", T, 50,
                       "4", "A", 2, "p", T, 0,
                       "4", "A", 2, "t", F, 0,
                       "4", "A", 2, "k", T, 0,
                       "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,
                       "6", "A", 2, "p", F, 0,
                       "6", "A", 2, "t", F, 0,
                       "6", "A", 2, "k", T, 0
) 

然后,您基本上绘制了两个条形图。通常情况下,您将左右各偏移position_nudge,以使每个主题彼此相邻。但是,由于position参数已经被使用(“堆叠”),因此需要将主体转换为数字变量,因此请直接使用as.numeric(subject) - 0.125as.numeric(subject) + 0.125进行微调,然后相应地设置x轴的格式,以便没人注意到

ggplot(exampledata,
       aes(fill = type, colour = group)) +
    geom_col(
        data = subset(exampledata, time == 1),
        aes(x = as.numeric(subject) - 0.125, y = n),
        width = 0.25
    ) +
    geom_col(
        data = subset(exampledata, time == 2),
        aes(x = as.numeric(subject) + 0.125, y = n),
        width = 0.25
    ) +
    scale_fill_manual(values=c("gray97", "gray87")) +
    scale_colour_manual(values = c("royalblue", "navyblue")) +
    scale_x_continuous(limits = c(0.5, 6.5),
                       breaks = (c(1:6))) +
    xlab("subjects")

答案 3 :(得分:1)

您可以尝试使用小平面,但要去除小平面形状

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

相关问题