0

I am trying to create a violin plot for a database with one dependent variable and three factors (CHANGE_TYPE, IA_TYPE, PRESENTATION). I can create a violin plot with 4 violins using:

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

which produces the following:

Violin plot 1

However, that leaves out one factor. What I would like to do is merge two such plots horizontally so that they share a further, wider X axis showing the values of the factor PRESENTATION (which is currently just being averaged across, I suspect).

Any idea how I might achieve that?

EDIT: Apologies for not including the data. It looks something like this.

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, and IA_TYPE are factors with two levels each.

OvertOddity
  • 7
  • 1
  • 6
  • you might want to use `facet_grid` with a formula like `.~PRESENTATION`. To better help you a sample dataset would be very welcome. – Wietze314 Feb 21 '17 at 12:09
  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – zx8754 Feb 21 '17 at 12:26
  • @Wietze314: My bad, I have now included a few rows of the dataset to give you an idea. – OvertOddity Feb 21 '17 at 14:17
  • 1
    Use `interaction` to create a new factor based on CHANGE_TYPE and PRESENTATION and use this new factor in ggplot. – AEF Feb 21 '17 at 14:19

1 Answers1

0

The solution is to combine two variables in one x axis factor, before plotting. You don't need to plot two plots subsequently, because with ggplot there are several options to do this in one step:

The data:

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)

The plots:

library(ggplot2)

solution 1, use facet_grid

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

solution 2, use interaction between both factors of interest

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

solution 1 solution 1 solution 2 solution 2

Wietze314
  • 5,942
  • 2
  • 21
  • 40