user702846 user702846 - 2 months ago 4
R Question

overlaying boxes with the same factor

In the following plot, for every factor (week) there is three boxes, enter image description here. However, I rather boxes from each week to be on top of each other. (The same X position), therefore, they will take less space.

Here is a toy dataset along wit the code,

dat <- read.table(text = ' animal_id condition week_number label variable value
1 12 adlib 1 <NA> pc.value -0.110826762
2 13 adlib 1 <NA> pc.value -0.037862740
3 14 adlib 1 <NA> pc.value -0.252474983
4 15 pair 1 <NA> pc.value 0.097274297
5 16 pair 1 <NA> pc.value -0.015273153
6 17 adlib 1 <NA> pc.value -0.069534177
7 18 pair 1 <NA> pc.value -0.033537590
8 1 con 1 <NA> pc.value 0.167484278
9 2 con 1 <NA> pc.value 0.183647180
10 19 con 1 <NA> pc.value 0.229669153
11 20 con 1 <NA> pc.value 0.229764374
12 3 con 1 3 pc.value 0.357263429
13 21 con 1 <NA> pc.value 0.220650230
14 22 con 1 22 pc.value 0.126420908
15 23 con 1 <NA> pc.value 0.297326296
16 24 con 1 <NA> pc.value 0.227480415
17 25 pair 1 <NA> pc.value -0.379717347
18 26 pair 1 <NA> pc.value -0.262408892
19 27 pair 1 <NA> pc.value -0.044676089
20 28 pair 1 <NA> pc.value -0.270336604
21 29 adlib 1 <NA> pc.value -0.220693336
22 30 adlib 1 <NA> pc.value -0.206574036
23 4 con 1 <NA> pc.value 0.264751685
24 32 adlib 1 <NA> pc.value -0.005645539
25 33 adlib 1 <NA> pc.value -0.374329026
26 34 pair 1 <NA> pc.value -0.201130564
27 36 adlib 1 <NA> pc.value -0.227777601
28 5 con 1 <NA> pc.value 0.233753789
29 6 con 1 <NA> pc.value 0.247092487
30 7 pair 1 <NA> pc.value -0.237548606
31 8 adlib 1 <NA> pc.value -0.158702696
32 9 adlib 1 <NA> pc.value -0.049164200
33 1 con 2 <NA> pc.value -0.248305928
34 2 con 2 <NA> pc.value -0.313808423
35 3 con 2 <NA> pc.value -0.240626126
36 4 con 2 <NA> pc.value -0.235823821
37 5 con 2 <NA> pc.value -0.360493457
38 6 con 2 <NA> pc.value -0.137924685
39 7 pair 2 <NA> pc.value 0.133718228
40 8 adlib 2 <NA> pc.value 0.061174814
41 9 adlib 2 <NA> pc.value 0.246007965
42 10 pair 2 <NA> pc.value 0.255396196
43 11 pair 2 11 pc.value -0.138252815
44 12 adlib 2 <NA> pc.value 0.105495107
45 13 adlib 2 <NA> pc.value 0.192678628
46 14 adlib 2 <NA> pc.value -0.051937483
47 15 pair 2 <NA> pc.value 0.102928123
48 16 pair 2 <NA> pc.value 0.164727853
49 17 adlib 2 <NA> pc.value -0.052299544
50 18 pair 2 <NA> pc.value 0.267744360
51 19 con 2 <NA> pc.value -0.213689714
52 20 con 2 <NA> pc.value -0.308084378
53 21 con 2 <NA> pc.value -0.292024696
54 22 con 2 <NA> pc.value -0.100708378
55 23 con 2 <NA> pc.value -0.374211596
56 24 con 2 <NA> pc.value -0.375983300
57 25 pair 2 <NA> pc.value 0.210358577
58 26 pair 2 <NA> pc.value 0.207683682
59 27 pair 2 <NA> pc.value 0.298564351
60 28 pair 2 <NA> pc.value 0.186188623
61 29 adlib 2 <NA> pc.value 0.107437391
62 30 adlib 2 <NA> pc.value 0.161707111
63 32 adlib 2 <NA> pc.value 0.052134760
64 33 adlib 2 <NA> pc.value 0.200857395
65 34 pair 2 <NA> pc.value 0.189955861
66 35 adlib 2 <NA> pc.value 0.263397668
67 36 adlib 2 <NA> pc.value 0.036017649',head=T, fill=T)


dat$week_number = as.character(dat$week_number)
dat$condition = as.character(dat$condition)
dat$label = as.character(dat$label)
plt <- ggplot(dat, aes(x = week_number, y = value)) +
geom_boxplot(aes(fill=condition))
plt

Answer

We need position = "identity" with alpha, still looks less than ideal:

ggplot(dat, aes(x = week_number, y = value, fill = condition)) + 
  geom_boxplot(position = "identity", alpha = 0.3) +

enter image description here