0

I have a histogram where x has 42 values. I used a facet_wrap to handle True/False values of a logical vector. It looks like this.

enter image description here

As you can see, I have mainly four-six x periods to worry about (i.e, tra 1 to 4 and test 1 & 2). What I want to do is to make two pie charts showing the relative frequency for trials above threshold (which I will probably adjust to have those categories) and the category "other" that completes 100% of the counts.

I believe my problem is with the data.frame I am using for the ggplot. In such case, how should I reshape my data? A subset of my data and the code for the plot

   dput(df[1:500,6:8])
structure(list(Entrenamiento = c("VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", 
"FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "FALSO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", "VERDADERO", 
"VERDADERO", "VERDADERO", "VERDADERO"), Maximo = c(219.545, 24.273, 
18.364, 5.864, 15.182, 142.545, 11.955, 1.455, 36.182, 146.182, 
17.682, 29.364, 24.136, 23.636, 30.955, 10.136, 30.045, 28.227, 
4.909, 37.636, 18.955, 55.136, 1.727, 4.5, 46.045, 0, 12.364, 
10.636, 115.5, 13.045, 122.455, 8.409, 1.727, 3.182, 0.409, 16.5, 
5.636, 13.864, 22.091, 23.455, 9.045, 18.045, 3, 90.727, 9.273, 
99.227, 17.364, 15.136, 7.636, 44.227, 67.909, 60.5, 0.909, 32.409, 
5.273, 5.727, 0, 3.864, 84.273, 0, 2.409, 28, 114.091, 26.091, 
13.773, 23.455, 2.591, 27.045, 21, 106.636, 14.136, 0.636, 3.045, 
64.773, 32.5, 102.227, 39.318, 21.909, 17.636, 5.727, 28.955, 
210, 31.455, 36, 0, 35.227, 79.682, 7.318, 41.955, 12, 98.5, 
183.864, 123.455, 113.045, 63.091, 1.273, 259.591, 177.773, 58.136, 
35.955, 5.364, 8.864, 20.273, 136.182, 70.273, 93, 48.545, 64.318, 
104.591, 177.864, 6.955, 31.455, 8.455, 132.136, 82.864, 21.227, 
0, 37.182, 213.318, 22.273, 6.273, 32.818, 8.545, 11.636, 18.636, 
30.045, 13.045, 10.727, 31.136, 8.273, 63.364, 31.909, 88.818, 
8.455, 48.409, 43.091, 54.818, 0.545, 23.773, 26.636, 21.636, 
22, 65.773, 95.682, 6.227, 0.773, 33.273, 35.636, 1.182, 39.591, 
1.182, 57.545, 80.955, 7.273, 24.773, 2.364, 69.13636364, 51.5, 
63.81818182, 88.13636364, 32.13636364, 176, 6.681818182, 7.318181818, 
99.95454545, 22.13636364, 26.72727273, 63.72727273, 82.22727273, 
110.7727273, 88.77272727, 24.77272727, 16.5, 54.95454545, 28.59090909, 
36, 6.045454545, 10.59090909, 26.77272727, 53.40909091, 62.27272727, 
141.2727273, 19.90909091, 17.59090909, 26.31818182, 57.31818182, 
72.18181818, 28.72727273, 56.68181818, 62.27272727, 65.68181818, 
65.86363636, 71.22727273, 20.86363636, 80.13636364, 1.045454545, 
53.31818182, 34.77272727, 27.72727273, 0.954545455, 31.95454545, 
12.18181818, 42.63636364, 50.54545455, 0, 20, 112.8181818, 94.18181818, 
6.545454545, 74.31818182, 9.818181818, 15.59090909, 13.40909091, 
45.77272727, 19.86363636, 11.31818182, 26.95454545, 6.136363636, 
4.318181818, 19.54545455, 0.772727273, 40, 16.77272727, 18.54545455, 
3.363636364, 0, 17.59090909, 5.909090909, 136.1363636, 39, 72, 
0, 36.27272727, 12.54545455, 2.681818182, 40.18181818, 61.13636364, 
13.13636364, 51.18181818, 0.409090909, 49.36363636, 105.1818182, 
56.09090909, 44.81818182, 147.5, 168.9545455, 41.86363636, 11.5, 
79.27272727, 3.590909091, 214.2272727, 12.95454545, 83.04545455, 
86.95454545, 0, 10.18181818, 0, 26.54545455, 65.27272727, 4, 
36.13636364, 25.95454545, 84.27272727, 41.54545455, 26.27272727, 
12.09090909, 59.72727273, 158.6363636, 64.95454545, 27.59090909, 
40.31818182, 19.45454545, 33.13636364, 62.36363636, 85.31818182, 
48.5, 64.13636364, 93.72727273, 56.86363636, 112, 128.9545455, 
12.72727273, 66.59090909, 142.3181818, 41.77272727, 15.54545455, 
23.27272727, 39, 0, 16.86363636, 146, 9.772727273, 15.36363636, 
8.681818182, 20.63636364, 36.45454545, 55.86363636, 126.6818182, 
10.36363636, 19.18181818, 9.545454545, 48.86363636, 69.31818182, 
45.13636364, 47.27272727, 71.59090909, 309.2272727, 36.95454545, 
44.86363636, 72.81818182, 27.09090909, 100.1363636, 23.81818182, 
37.77272727, 27.22727273, 131.4545455, 14.27272727, 14.09090909, 
54.27272727, 50.40909091, 54.45454545, 57.27272727, 238.7727273, 
14.77272727, 54.81818182, 83.81818182, 78.18181818, 25.04545455, 
14.31818182, 136.1818182, 66.27272727, 43.59090909, 164.8181818, 
94.18181818, 148.4090909, 50.54545455, 72.86363636, 14.77272727, 
134.5909091, 8.090909091, 31.40909091, 35.86363636, 32.22727273, 
13.22727273, 116.5454545, 17.86363636, 85.27272727, 71.09090909, 
155.6818182, 183, 69.18181818, 86.72727273, 71.27272727, 13.04545455, 
221.8636364, 114.5909091, 6.136363636, 106.9090909, 10.45454545, 
19.59090909, 92.40909091, 147.6818182, 80.45454545, 24, 24.18181818, 
20.63636364, 27.81818182, 11.77272727, 15.59090909, 47.59090909, 
57.77272727, 89.68181818, 98.22727273, 24.81818182, 19.04545455, 
145.9090909, 11.18181818, 7.681818182, 36.86363636, 139.8181818, 
73.5, 11.04545455, 87.09090909, 5.590909091, 70.22727273, 8.181818182, 
102.8636364, 108.1363636, 71.81818182, 35.86363636, 29.90909091, 
80.36363636, 188.8636364, 16.54545455, 8.454545455, 22.31818182, 
36.90909091, 123.2727273, 56.09090909, 76.36363636, 9.590909091, 
164.4545455, 179.8636364, 209.0454545, 38.86363636, 40.63636364, 
22.68181818, 30.13636364, 17.22727273, 41.86363636, 8.227272727, 
63.40909091, 54.77272727, 63.18181818, 58.5, 23.72727273, 278.3181818, 
26.31818182, 142.3181818, 303.6363636, 18.18181818, 15.22727273, 
73.04545455, 185.0909091, 98.81818182, 200.3636364, 11.72727273, 
60.5, 154, 55.27272727, 115.4090909, 17.09090909, 20.18181818, 
26.77272727, 47, 61.40909091, 151.6363636, 29.68181818, 5.818181818, 
118.5454545, 66.63636364, 19.22727273, 3.409090909, 13.22727273, 
7.454545455, 186.9545455, 54.45454545, 13, 126.6363636, 223.1363636, 
7.409090909, 50.5, 48.36363636, 118.9545455, 13.04545455, 183.1363636, 
74.5, 23.68181818, 62.81818182, 101.9545455, 101.3636364, 23.27272727, 
10.5, 19.5, 58.04545455, 2.727272727, 22.90909091, 78.54545455, 
15.86363636, 4.863636364, 13.22727273, 2.636363636, 64.63636364, 
3.954545455, 42.72727273, 15.77272727, 63.86363636, 73.59090909, 
94.54545455, 177.4090909, 17, 77, 57.22727273, 214.2272727, 38.72727273, 
24, 154.6363636, 29.63636364, 150.5909091, 34.72727273, 264.5909091, 
22.5, 30.5, 192.4545455, 29.86363636, 17.54545455, 6.954545455, 
39.13636364, 20.27272727, 47.59090909), Pasaje = structure(c(2L, 
1L, 31L, 2L, 31L, 2L, 4L, 31L, 31L, 2L, 2L, 1L, 31L, 31L, 32L, 
2L, 31L, 2L, 2L, 32L, 31L, 31L, 32L, 32L, 2L, 1L, 31L, 2L, 32L, 
4L, 4L, 3L, 1L, 1L, 2L, 1L, 31L, 31L, 32L, 4L, 31L, 31L, 31L, 
32L, 31L, 32L, 32L, 31L, 2L, 32L, 31L, 32L, 2L, 32L, 4L, 32L, 
1L, 32L, 32L, 1L, 31L, 32L, 32L, 3L, 32L, 32L, 32L, 32L, 31L, 
32L, 1L, 3L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 4L, 32L, 2L, 
31L, 32L, 1L, 1L, 31L, 32L, 1L, 1L, 31L, 1L, 32L, 31L, 32L, 2L, 
32L, 31L, 31L, 1L, 3L, 2L, 3L, 4L, 31L, 2L, 31L, 32L, 2L, 32L, 
2L, 32L, 1L, 31L, 31L, 31L, 1L, 32L, 31L, 31L, 32L, 2L, 32L, 
32L, 31L, 31L, 31L, 31L, 31L, 2L, 31L, 31L, 31L, 4L, 32L, 32L, 
32L, 32L, 32L, 31L, 32L, 32L, 32L, 32L, 31L, 31L, 31L, 32L, 31L, 
31L, 1L, 31L, 32L, 31L, 31L, 1L, 1L, 2L, 2L, 4L, 2L, 32L, 31L, 
31L, 4L, 3L, 32L, 2L, 31L, 2L, 2L, 31L, 4L, 2L, 32L, 2L, 2L, 
1L, 2L, 32L, 2L, 31L, 31L, 31L, 4L, 2L, 31L, 1L, 31L, 3L, 2L, 
2L, 32L, 31L, 1L, 4L, 32L, 32L, 32L, 32L, 32L, 31L, 31L, 32L, 
1L, 31L, 3L, 31L, 31L, 32L, 32L, 31L, 32L, 31L, 31L, 31L, 32L, 
31L, 1L, 3L, 31L, 32L, 32L, 32L, 31L, 1L, 2L, 2L, 32L, 32L, 32L, 
1L, 32L, 31L, 1L, 32L, 2L, 1L, 1L, 1L, 1L, 1L, 32L, 4L, 2L, 3L, 
4L, 1L, 2L, 4L, 1L, 2L, 2L, 2L, 1L, 32L, 1L, 31L, 31L, 31L, 1L, 
1L, 31L, 31L, 31L, 31L, 1L, 31L, 2L, 1L, 2L, 31L, 2L, 2L, 3L, 
32L, 3L, 4L, 32L, 31L, 31L, 32L, 32L, 31L, 31L, 3L, 32L, 31L, 
1L, 31L, 32L, 1L, 32L, 1L, 31L, 32L, 1L, 1L, 1L, 3L, 1L, 1L, 
4L, 2L, 1L, 2L, 2L, 2L, 1L, 4L, 4L, 32L, 1L, 32L, 1L, 2L, 32L, 
28L, 31L, 2L, 1L, 32L, 4L, 24L, 2L, 2L, 2L, 1L, 2L, 4L, 7L, 14L, 
1L, 31L, 4L, 31L, 31L, 17L, 4L, 19L, 8L, 32L, 20L, 32L, 2L, 1L, 
1L, 2L, 2L, 31L, 4L, 2L, 32L, 31L, 4L, 2L, 9L, 31L, 8L, 31L, 
31L, 32L, 31L, 31L, 32L, 31L, 31L, 4L, 1L, 31L, 31L, 32L, 32L, 
32L, 29L, 31L, 31L, 19L, 31L, 32L, 31L, 32L, 32L, 10L, 31L, 13L, 
31L, 32L, 32L, 31L, 31L, 31L, 32L, 32L, 31L, 26L, 1L, 32L, 3L, 
4L, 1L, 1L, 1L, 32L, 2L, 2L, 31L, 2L, 2L, 4L, 1L, 31L, 10L, 10L, 
2L, 5L, 31L, 32L, 19L, 2L, 2L, 2L, 20L, 31L, 1L, 31L, 31L, 2L, 
4L, 3L, 23L, 12L, 31L, 31L, 1L, 1L, 31L, 31L, 12L, 32L, 31L, 
31L, 31L, 31L, 32L, 31L, 31L, 26L, 31L, 32L, 3L, 32L, 32L, 32L, 
2L, 32L, 32L, 31L, 32L, 31L, 32L, 32L, 17L, 31L, 31L, 30L, 31L, 
31L, 31L, 30L, 1L, 25L, 31L, 31L, 32L, 31L, 1L, 32L, 32L, 2L, 
32L, 2L, 31L, 2L, 1L, 1L, 2L, 1L, 2L, 31L, 32L, 31L, 1L, 2L, 
2L, 31L, 1L, 31L, 2L, 1L), .Label = c("tra1", "tra2", "tra3", 
"tra4", "tra5", "tra6", "tra7", "tra8", "tra9", "tra10", "tra11", 
"tra12", "tra13", "tra14", "tra15", "tra16", "tra17", "tra18", 
"tra19", "tra20", "tra21", "tra22", "tra23", "tra24", "tra25", 
"tra26", "tra27", "tra28", "tra29", "tra30", "test1", "test2", 
"test3", "test4", "test5", "test6", "test7", "test8", "test9", 
"test10", "test11", "test12"), class = "factor")), .Names = c("Entrenamiento", 
"Maximo", "Pasaje"), row.names = c(NA, 500L), class = "data.frame")

    ggplot(df,aes(Pasaje))+
      geom_histogram()+ theme_bw()+
      theme(axis.text.x = element_text(angle = 90, hjust = 1))+
      facet_wrap(~ Entrenamiento)

I have looked here for answers related but I still can't manage to understand the kind of histogram I should be using to get the job done.

Pie charts in ggplot2 with variable pie sizes

Converting bar chart to pie chart in R

http://www.sthda.com/english/wiki/ggplot2-pie-chart-quick-start-guide-r-software-and-data-visualization

http://docs.ggplot2.org/current/coord_polar.html

UPDATE 2

I have found useful code here

It almost does the job, I am trying to modify the threshold part to get the output.

# Your data
# get counts and melt it

frec<-table(df$Pasaje,df$Entrenamiento)
data<-melt(frec)
names(data)<-c("Pasaje","Entrenamiento","Frecuencia")


# calculate percentage:
m1 = ddply(data, .(Entrenamiento), summarize, ratio=Frecuencia/sum(Frecuencia))

#order data frame (needed to comply with percentage column):
m2 = data[order(data$Entrenamiento),]

# combine them:
mydf = data.frame(m2,ratio=m1$ratio)

# get positions of percentage labels:
mydf = ddply(mydf, .(Entrenamiento), transform, position = cumsum(Frecuencia) - 0.5*Frecuencia) 

# Establish threshold

thres<-0.01 # Play here to define % desired
mydf2 = mydf[which(mydf$ratio>thres),]
for (i in mydf2$Pasaje){  # Maybe not the best approach
if (grep(i,mydf$Pasaje)==F){
  mydf$Pasaje2<-"otro"
} else{
  v<-grep(i,mydf$Pasaje)
  mydf$Pasaje2[v]<-i      
  }
}


# create bar plot
pie = ggplot(mydf, aes(x = factor(1), y = Frecuencia, fill = as.factor(Pasaje2))) +
  geom_bar(stat = "identity", width = 1) +
  facet_wrap(~Entrenamiento)

# make a pie
pie = pie + coord_polar(theta = "y")

# add labels
pie + geom_text(aes(label = sprintf("%1.2f%%", 100*ratio), y = position))
Community
  • 1
  • 1
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • What exactly are you trying to summarize? The histogram itself is already a "summary". – hrbrmstr Oct 10 '15 at 15:58
  • Please don't use a pie chart. They. Are. Awful. – alexwhitworth Oct 10 '15 at 16:00
  • @Alex I am eager to find another way to show what I want, any ideas? – Matias Andina Oct 10 '15 at 16:02
  • @hrbrmstr I understand that histogram is already a summary, as I stated I want to have an easy comparable % values of 4-6 categories that I will color with a gradient. My goal is to communicate x% of events occur here there and there easily (which account for more than 90%) the rest is crap that shouldn't be distracting you. That histogram is not doing that job, however I think I will still have to show both histogram and pie chart – Matias Andina Oct 10 '15 at 16:06
  • Can you `dput` more (or link to data) (or include some that have more levels for `Entrenamiento`)? I'm thinking a heat map may be a better approach. – hrbrmstr Oct 10 '15 at 16:12
  • @hrbrmstr It might be a great idea! I edited the question and now there's a dput of half of my data – Matias Andina Oct 10 '15 at 16:27
  • And, to confirm, you aren't using `Maximo` at all? – hrbrmstr Oct 10 '15 at 16:38
  • @hrbrmstr Well maximo (max value) is a variable of interest that I'm not using here. It was easier to subset from 6:8. It could be interesting to add another layer of how high was and when. I thoguht it was too much complexity so I performed univariated analysis. Feel free to play with it, heatmaps sound like it could be included. – Matias Andina Oct 10 '15 at 16:43
  • I think that if you use a bar chart and make the following changes, you'll get your point across: (1) use percentages vs counts (or make two plots, one of percentages and one of counts), (2) transpose X and Y axes, (3) order the % (counts) in decreasing order. You could also always make a sub-plot of only the important categories... there is no need for a pie chart – alexwhitworth Oct 10 '15 at 17:52
  • @Alex I have already made: 1) Stacked histogram of Entrenamiento values, 2) Decreasing order (although it doesn't make too much sense because there's a natural time order in x axis) 3) coord_flip. None of those options completely satisfies me, I guess it works as a whole. Even with six categories, the pie charts I've made are not easy to read – Matias Andina Oct 10 '15 at 18:24

3 Answers3

3

I think this gets you closer to what you want:

library(dplyr)
library(tidyr)
library(scales)
library(ggplot2)
library(viridis)

count(df, Entrenamiento, Pasaje) %>% 
  mutate(pct=n/sum(n)) %>% 
  ungroup() %>% 
  complete(Pasaje, Entrenamiento) %>% 
  ggplot(aes(x=Pasaje, y=Entrenamiento, fill=pct)) +
  geom_tile(color="#7f7f7f", size=0.1) +
  coord_fixed() +
  scale_fill_viridis(name="Percent", label=percent, na.value="white") +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(axis.text.x=element_text(angle=90, hjust=1)) +
  theme(panel.grid=element_blank())

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
1

In general, pie charts are a bad idea. ggplot2 deliberately didn't include them as an easy output for this reason.

If you really must, here's a quick method, using your data:

library(ggplot2)

ggplot(df, aes(x = factor(1), fill = factor(Pasaje))) +
           geom_bar(width = 1) +
           coord_polar(theta = "y")

enter image description here

What we have done, is made a stacked barplot with width = 1, then turned it into a piechart by wrapping it using coord_polar.

The help for coord_polar retierates that this is not a good idea:

NOTE: Use these plots with caution - polar coordinates has major perceptual problems. The main point of these examples is to demonstrate how these common plots can be described in the grammar. Use with EXTREME caution.

You could also do it in base:

pie(table(df$Pasaje), labels=names(table(df$Pasaje)))
enter image description here

jeremycg
  • 24,657
  • 5
  • 63
  • 74
  • I am aware that piecharts might be confusing. However, I can't come up with another form of representation for summarizing the 42 level histogram. I believe information is there and you can manage to understand the almost unimodal (test1 test2) vs the "bi-modal" tra 1 to 4 and test 1 & 2. But it requires effort from the reader and extra calculations. Instead, a pie chart will show relative amount with color and it would be easier to see using a gradient. – Matias Andina Oct 10 '15 at 15:21
  • I'd rather prefer keep ggplot graphics for consistency with everything else. Also, the pie is for counts and includes all my levels, how can I make a threshold and use relative frequencies or percent (Entrenamiento groups have different sizes and counts make one of the pies stay open) – Matias Andina Oct 10 '15 at 15:25
1

Here you go. I've entered your dput data into a data.frame called x.

I didn't bother with your custom theme, but you can obviously add it.

library(ggplot2)
library(scales)
library(plyr)

x2 <- ddply(x, .var= c("Pasaje", "Entrenamiento"), function(x) {return(nrow(x))})

ggplot(x2,aes(x=reorder(Pasaje, V1), y= V1))+
  geom_bar(stat= "identity")+ coord_flip() +
  facet_wrap(~ Entrenamiento) + scale_y_continuous(labels= percent) +
  labs(x= "Pasaje", y= "Percent of Counts")

ggplot(x2,aes(x=reorder(Pasaje, V1), y= V1))+
  geom_bar(stat= "identity")+ coord_flip() +
  facet_wrap(~ Entrenamiento) + 
  labs(x= "Pasaje", y= "Counts")

enter image description here enter image description here

Edit

If you don't want to reorder the axis because of the "natural time order" not specified originally, you can always remove the call to reorder

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
  • I'm sorry about, not mentioning that, heads up for your help, I was writing the comment when you posted the answer! I notice that percent values are different from what I've calculated. I guess I used percent values per group and not as a whole like you, could it be ? – Matias Andina Oct 10 '15 at 18:31
  • Yes, I think that's the way that `stat_identity` and `facet_wrap` work together, though I'm not sure. You could always calculate the percentages manually instead of using `scale_y_continuous` – alexwhitworth Oct 10 '15 at 19:04