我正在尝试为一个数据库创建一个小提琴图,其中包含一个因变量和三个因子(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是各自有两个级别的因素。
答案 0 :(得分:0)
解决方案是在绘图之前将两个变量组合在一个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))