结合R中的小提琴图

Combining violin plots in R

我正在尝试为具有一个因变量和三个因子(CHANGE_TYPE、IA_TYPE、PRESENTATION)的数据库创建一个小提琴图。我可以使用 4 把小提琴创建一个小提琴情节:

p <- ggplot(data, aes(factor(CHANGE_TYPE), mean_reading_rate))
p <- p + geom_violin(aes(fill = IA_TYPE))

产生以下结果:

然而,这遗漏了一个因素。我想做的是水平合并两个这样的图,以便它们共享一个更宽的 X 轴,显示因子 PRESENTATION 的值(我怀疑目前只是对其进行平均)。

知道如何实现吗?

编辑:抱歉没有包含数据。它看起来像这样。

Participant CHANGE_TYPE PRESENTATION    IA_TYPE mean_reading_rate stdev_reading_rate
        1  Accidental            1 non_target          80.73478           83.58174
        1  Accidental            1     target          50.50102           32.69693
        1  Accidental            2 non_target          73.10850           78.39836
        1  Accidental            2     target          54.38447           45.92599
        1 Substantive            1 non_target          68.96310           64.04057
        1 Substantive            1     target          63.66268           25.35560
        1 Substantive            2 non_target          60.08031           71.11967
        1 Substantive            2     target          66.63825           59.14888
        2  Accidental            1 non_target          56.52367           61.98396
        2  Accidental            1     target          44.49227           40.71171
        2  Accidental            2 non_target          68.21995           79.33617
        2  Accidental            2     target          47.27487           28.70954
        2 Substantive            1 non_target          53.29330           65.13060
        2 Substantive            1     target          33.96295           21.49306
        2 Substantive            2 non_target          51.28319           62.61050
        2 Substantive            2     target          59.85926           63.77074
        3  Accidental            1 non_target          56.25112           64.13430
        3  Accidental            1     target          34.22665           18.58870
        3  Accidental            2 non_target          47.78169           64.05134
        3  Accidental            2     target          45.62304           79.84651
        3 Substantive            1 non_target          54.82215           69.43809
        3 Substantive            1     target          39.66745           22.40827
        3 Substantive            2 non_target          40.58735           61.09965
        3 Substantive            2     target          73.39946           80.98760

CHANGE_TYPE、PRESENTATION 和 IA_TYPE 是各有两个水平的因素。

解决方案是在绘图之前将两个变量合并到一个 x 轴因子中。 您不需要随后绘制两个图,因为使用 ggplot 有多个选项可以一步完成:

数据:

str <- '
Participant CHANGE_TYPE PRESENTATION    IA_TYPE mean_reading_rate stdev_reading_rate
1  Accidental            1 non_target          80.73478           83.58174
1  Accidental            1     target          50.50102           32.69693
1  Accidental            2 non_target          73.10850           78.39836
1  Accidental            2     target          54.38447           45.92599
1 Substantive            1 non_target          68.96310           64.04057
1 Substantive            1     target          63.66268           25.35560
1 Substantive            2 non_target          60.08031           71.11967
1 Substantive            2     target          66.63825           59.14888
2  Accidental            1 non_target          56.52367           61.98396
2  Accidental            1     target          44.49227           40.71171
2  Accidental            2 non_target          68.21995           79.33617
2  Accidental            2     target          47.27487           28.70954
2 Substantive            1 non_target          53.29330           65.13060
2 Substantive            1     target          33.96295           21.49306
2 Substantive            2 non_target          51.28319           62.61050
2 Substantive            2     target          59.85926           63.77074
3  Accidental            1 non_target          56.25112           64.13430
3  Accidental            1     target          34.22665           18.58870
3  Accidental            2 non_target          47.78169           64.05134
3  Accidental            2     target          45.62304           79.84651
3 Substantive            1 non_target          54.82215           69.43809
3 Substantive            1     target          39.66745           22.40827
3 Substantive            2 non_target          40.58735           61.09965
3 Substantive            2     target          73.39946           80.98760
'

file <- textConnection(str)

data <- read.table(file, header = T)

地块:

library(ggplot2)

解决方案 1,使用 facet_grid

p <- ggplot(data, aes(factor(CHANGE_TYPE), mean_reading_rate))
p <- p + geom_violin(aes(fill = IA_TYPE))
p + facet_grid(.~PRESENTATION)

解决方案 2,在两个感兴趣的因素之间使用 interaction

ggplot(data, aes(factor(interaction(CHANGE_TYPE,PRESENTATION)), mean_reading_rate)) + 
  geom_violin(aes(fill = IA_TYPE))

解决方案 1 解决方案 2