Issue with splitting data in R - r

I am trying to run a code that would enable me to make a comparison between the current versus the past (last 6 months) socioeconomic situation and the voting preference in the last Spanish General Elections, which were in 2019.
If I run this code, I get the correct graphical output for all the parties that participated in the aforementioned elections:
#Analysis of Question 3 (current versus past situation of Spanish consumers, their perception of their own situation) in addition to Question 31a (facet of the respondents' voting preference)
library(ggplot2)
library(dplyr)
set.seed(123)
CIS_data_6 <- data.frame(
CIS$RECUERDO,
CIS$P3
)
dput(CIS_data_6)
CIS_data_6$CIS.P3 <- sub("\\(NO LEER\\) ", "", CIS_data_6$CIS.P3)
labs <- c("Mejor", "Igual", "N.C.", "N.S.", "Peor")
CIS_data_6$CIS.P3 <- factor(CIS_data_6$CIS.P3, labels = labs)
Recuerdo <- CIS_data_6$CIS.RECUERDO
Recuerdolabs <- c("N.P.", "PSOE", "PP", "VOX", "Unidas Podemos", "Ciudadanos")
cut_points <- c(0, 2, 1, 18, c(21, 6, 67), 4)
i <-c(Recuerdo, !is.na(cut_points))
Recuerdo_fac <- Recuerdolabs[i]
Recuerdo_fac[is.na(Recuerdo)] <- Recuerdolabs[length(Recuerdolabs)]
Recuerdo_fac <- factor(Recuerdo_fac, levels = Recuerdolabs)
CIS_data_6$CIS.RECUERDO <- Recuerdo
ggplot(CIS_data_6, aes(Recuerdo)) +
geom_bar() +
xlab("Current VS Past Individual Situation Based on Voting Preference") +
ylab("Count") +
coord_flip() +
facet_wrap(~ CIS.P3) +
theme_bw()
However, this is graphically a mess, since there is all kind of minor parties which are of no relevance to my analysis.
The ones that I do want to outline are the big ones (plus abstention): "N.P." (Abstention), "PSOE", "PP", "VOX", "Unidas Podemos" and "Ciudadanos", with their corresponding encoding in the survey, which I denoted as cut_points: c(0, 2, 1, 18, c(21, 6, 67), 4).
Does anyone know how can I modify my code so that I can get to the desired output, i.e., to have a faceted graph (5 graphs, to be precise) saying "Mejor" ('Better'), "Peor" ('Worse'), "Igual" ('Same'), "N.S." ('Don't Know'), "N.C." ('Don't Answer') on the x-axis in each graph, and the political parties being located on the y-axis?
Many, many thanks in advance!
P.S. I am relatively new to this forum, so please, in case I have not expressed myself correctly or if there is any information lacking in my question, just let me know so, and I will be happy to provide it.
EDIT
dput(CIS_data_6)
is, as follows:
structure(list(CIS.RECUERDO = structure(c(2L, 2L, 28L, 1L, 2L,
1L, 15L, 15L, 2L, 26L, NA, 28L, 28L, 26L, 2L, NA, 13L, 2L, 27L,
2L, 15L, 1L, 27L, NA, 2L, 1L, NA, 1L, 2L, 15L, 25L, 26L, 26L,
26L, 2L, 1L, 15L, 15L, 24L, 1L, 15L, 28L, 2L, 2L, 24L, 3L, NA,
1L, NA, 27L, 1L, 27L, 26L, 2L, 2L, 28L, NA, 15L, 24L, 23L, 27L,
23L, 28L, 1L, 23L, 3L, 1L, 21L, 26L, 27L, 2L, 2L, 3L, 2L, 1L,
2L, 2L, 28L, 28L, 28L, 1L, 15L, 15L, 1L, 1L, 26L, 23L, 2L, 2L,
15L, 15L, 2L, 28L, 28L, 2L, 1L, 1L, 25L, 28L, 28L, 26L, 23L,
1L, 28L, 27L, 1L, 28L, 15L, 2L, 3L, 27L, 1L, 1L, 1L, 15L, 1L,
2L, 1L, 1L, 28L, 28L, 15L, 13L, 1L, 1L, 2L, 3L, 3L, 15L, 3L,
1L, 1L, 28L, 1L, 23L, 26L, 1L, 2L, 1L, 2L, 1L, 27L, 27L, NA,
28L, 26L, 21L, 3L, 28L, 2L, 26L, 2L, 26L, 1L, 26L, 2L, 15L, 3L,
2L, 1L, 15L, 1L, 2L, 2L, 28L, 2L, 28L, 15L, 1L, 2L, 2L, 15L,
NA, 28L, 1L, 1L, 28L, 28L, 15L, 28L, 1L, 1L, 26L, 28L, 2L, 2L,
26L, 28L, 1L, 28L, 3L, 1L, 13L, 26L, 15L, 28L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 1L, 28L, 26L, 1L, 28L, 28L, 28L, 27L, 23L, 13L, 2L,
27L, 2L, 26L, 2L, 15L, 28L, 1L, 2L, 2L, 24L, 2L, 26L, 2L, 27L,
1L, 28L, 15L, 15L, 13L, 21L, 28L, 13L, NA, 27L, 1L, 23L, 2L,
28L, 1L, 28L, 2L, 15L, 1L, 1L, 28L, 1L, 27L, 24L, 1L, 26L, 26L,
13L, 1L, 26L, 26L, 15L, 28L, 2L, 1L, 15L, 24L, 1L, 28L, 28L,
1L, 3L, 1L, 2L, 15L, 23L, 25L, 28L, 26L, 1L, 27L, 1L, 28L, 1L,
13L, 15L, 23L, 1L, 1L, 28L, 1L, 25L, 2L, 1L, 2L, 26L, 2L, NA,
26L, 25L, 25L, 2L, 2L, 28L, 13L, 15L, 23L, 26L, 2L, 1L, 24L,
2L, 1L, 1L, NA, 28L, 2L, 15L, 2L, 1L, 2L, 1L, 2L, 24L, 28L, 1L,
15L, 2L, 28L, 25L, 1L, 2L, 1L, 27L, 15L, 28L, 2L, 28L, 23L, 22L,
1L, NA, 3L, 2L, 28L, NA, 21L, 1L, 26L, 26L, 2L, 1L, 27L, 1L,
2L, 1L, 26L, 1L, 26L, 24L, 1L, 13L, 26L, 28L, 26L, 26L, 2L, 24L,
27L, 3L, 1L, 2L, 28L, 28L, 27L, 26L, 27L, 23L, 26L, 15L, 2L,
2L, 15L, 27L, 1L, 2L, 1L, 26L, 28L, 3L, 2L, 2L, 1L, 26L, 2L,
2L, 3L, 2L, 2L, 26L, 1L, 2L, 2L, 2L, 3L, 23L, 1L, 3L, 26L, 23L,
15L, 1L, 28L, 2L, 15L, 23L, 26L, 1L, 13L, 15L, 2L, 26L, 2L, 26L,
15L, 2L, 25L, 1L, 26L, 3L, 26L, 2L, 2L, 2L, 3L, 15L, 15L, 28L,
26L, 2L, 3L, 15L, 22L, 2L, 13L, 1L, 1L, 13L, 1L, 2L, 28L, 13L,
26L, 2L, 27L, 2L, 2L, 27L, 3L, 1L, 26L, 28L, 27L, 2L, 1L, 1L,
23L, 26L, 28L, 1L, 2L, 24L, 1L, 28L, 27L, 24L, 1L, 2L, 25L, 1L,
13L, 1L, 26L, 13L, NA, 3L, 28L, 15L, 1L, 15L, 15L, 2L, 28L, 2L,
2L, 2L, 26L, 3L, 25L, 3L, 2L, 1L, 28L, 25L, 26L, 2L, 28L, 27L,
1L, 2L, 24L, 27L, 23L, 2L, 13L, 2L, 27L, 13L, 13L, 2L, 1L, 1L,
25L, 13L, 28L, 22L, 2L, 23L, 28L, 27L, NA, 28L, 2L, 2L, 13L,
2L, 2L, 28L, 2L, 22L, 26L, 2L, NA, 28L, 2L, 2L, 28L, 13L, 2L,
27L, 28L, 3L, 2L, 2L, 24L, 15L, 2L, 23L, 2L, 1L, NA, 1L, 15L,
2L, 28L, 2L, 2L, 2L, 26L, 1L, 1L, 26L, 26L, 15L, 2L, 26L, 2L,
28L, 3L, 27L, 13L, 24L, 13L, 2L, 1L, 2L, 2L, 1L, 25L, 23L, 2L,
26L, 24L, 15L, 15L, 2L, 28L, 26L, 2L, 26L, 2L, 26L, 27L, 28L,
15L, 3L, 2L, 13L, 21L, 15L, 2L, 2L, 1L, NA, 26L, 1L, 2L, NA,
26L, 28L, 1L, 28L, 3L, 2L, 13L, 28L, 15L, 25L, 2L, 13L, 2L, 1L,
13L, 2L, 2L, 2L, 24L, 26L, NA, 2L, 28L, 28L, 1L, 15L, NA, 22L,
26L, 28L, 26L, 28L, 26L, 1L, 2L, 2L, 13L, 2L, 2L, 1L, 2L, 28L,
28L, 3L, 27L, 1L, 1L, NA, 26L, 28L, 2L, 2L, 2L, 2L, 15L, 26L,
3L, 28L, 25L, 28L, 15L, 1L, 26L, 2L, 28L, 26L, 1L, 15L, 26L,
1L, 8L, NA, 2L, 2L, 13L, 28L, 27L, 1L, 23L, 2L, 28L, 24L, 2L,
2L, 28L, 2L, 2L, 28L, 1L, NA, NA, 26L, 3L, 3L, 24L, 28L, 28L,
28L, 27L, 2L, 2L, 2L, 1L, 24L, 15L, 27L, 1L, 1L, 2L, 26L, 28L,
28L, 26L, 1L, 15L, 15L, NA, 2L, 15L, 27L, 28L, 2L, 26L, 24L,
28L, 26L, 23L, 26L, 1L, 26L, 23L, 26L, 2L, 27L, 13L, 28L, 27L,
24L, 26L, 26L, 27L, 26L, 26L, 15L, 2L, 13L, 2L, 3L, 15L, 1L,
1L, 1L, 2L, 2L, 26L, 26L, 2L, 2L, NA, 24L, 15L, 26L, 24L, 27L,
28L, 2L, 2L, 2L, 24L, 27L, 2L, 2L, 28L, 26L, 26L, 26L, 23L, 1L,
2L, 28L, 24L, NA, 13L, 2L, 26L, 3L, 25L, 1L, NA, 3L, 26L, 1L,
25L, 2L, 28L, 26L, 2L, 13L, 2L, 1L, 15L, 26L, 15L, 2L, 2L, 1L,
2L, 15L, 2L, 3L, 23L, 2L, 1L, 28L, 1L, 23L, 28L, 28L, 2L, 1L,
NA, 2L, 1L, 15L, 1L, 2L, 24L, 2L, 2L, 1L, NA, 25L, 3L, 2L, 26L,
13L, 2L, 28L, 1L, 13L, 1L, 27L, 15L, 2L, 25L, 2L, 1L, 1L, 28L,
28L, 26L, 21L, 27L, 26L, 1L, 1L, 3L, NA, 28L, 15L, 28L, 2L, 1L,
2L, 2L, 28L, 2L, 15L, 2L, 27L, 13L, 2L, 13L, 2L, 21L, 13L, 2L,
13L, 2L, 2L, 26L, 28L, 24L, 24L, 2L, 24L, 28L, 13L, 15L, 1L,
2L, 15L, NA, 2L, 27L, 15L, 15L, 1L, 2L, 2L, 2L, 28L, 26L, 1L,
27L, 1L, 28L, 26L, 2L, 1L, 1L, 23L, 24L, 15L, 2L, 1L, 28L, 25L,
28L, 27L, 28L, 1L, 15L, 21L, 27L, 1L, 2L, 1L, 28L, 28L, 24L,
1L, 2L, 2L, 23L, 28L, 23L, 28L, 2L, 2L, NA, 2L, 26L, 26L, 2L,
1L, 1L, 2L, 27L, 2L, 1L, 28L, 2L, 28L, 26L, 28L, NA, 13L, 2L,
2L, NA, 28L, 1L, 3L, 13L, 13L, 2L, 2L, 21L, 15L, 15L, 1L, 28L,
2L, 1L, 13L, 2L, 2L, 26L, 26L, 24L, 3L, 28L, 3L, 15L, 2L, 22L,
27L, 26L, 26L, 1L, 28L, 2L, 28L, 2L, 28L, 2L, 1L, 1L, 1L, 28L,
2L, 15L, 2L, 2L, 2L, NA, 27L, 1L, 2L, 22L, 1L, 27L, 2L, 13L,
1L, 2L, 2L, NA, 3L, 26L, 1L, 2L, 2L, 2L, 26L, 15L, 27L, 28L,
15L, 3L, 28L, 28L, 15L, 26L, NA, 2L, 28L, 28L, 26L, 26L, 2L,
28L, 13L, 28L, 25L, 2L, 27L, 2L, 1L, 13L, 26L, 28L, 26L, 2L,
15L, 26L, 1L, 26L, 26L, 1L, 13L, 1L, 13L, 1L, 26L, 15L, 26L,
1L, 2L, 2L, 2L, 28L, 3L, 28L, 2L, 26L, 13L, 2L, 23L, 2L, 3L,
15L, NA, 2L, NA, 26L, 6L, 2L, 3L, 3L, 2L, 3L, 28L, 27L, 21L,
4L, 6L, 1L, 22L, 28L, 27L, 26L, 4L, 28L, 27L, NA, 26L, 6L, 6L,
2L, 6L, 28L, 7L, 2L, 26L, 27L, NA, 2L, 26L, 2L, 26L, 28L, 4L,
2L, 27L, 6L, 3L, 24L, 27L, 2L, 2L, NA, 23L, 28L, 3L, 27L, 1L,
2L, 27L, 26L, 24L, NA, 27L, 2L, 4L, 4L, 2L, 2L, 24L, 27L, NA,
2L, 2L, NA, 26L, 4L, 6L, NA, 28L, 26L, 4L, 4L, 3L, 24L, 23L,
7L, 2L, 25L, 27L, 2L, 4L, 28L, NA, 2L, 6L, 2L, 2L, 2L, 13L, 6L,
26L, 2L, 2L, 24L, 2L, NA, 28L, 2L, 6L, 1L, 4L, 4L, 3L, 2L, 27L,
2L, NA, 2L, 2L, 7L, 2L, 7L, 28L, NA, 2L, 2L, 27L, 2L, 2L, NA,
3L, 2L, 26L, 27L, 2L, 6L, 1L, 2L, NA, 6L, 26L, 24L, 4L, 4L, 4L,
28L, 4L, 4L, 4L, 4L, 13L, 7L, 26L, 28L, 27L, 6L, NA, NA, 28L,
6L, 26L, 2L, 4L, 24L, 26L, 2L, 1L, 21L, 28L, 7L, 27L, 26L, 28L,
2L, 27L, 26L, 2L, 4L, 26L, 2L, 1L, 27L, 26L, 7L, 28L, 26L, 28L,
27L, 3L, NA, 27L, 28L, 4L, 2L, 4L, 26L, 2L, 7L, 26L, 1L, 2L,
1L, 2L, 6L, 26L, 2L, 2L, 7L, 4L, 26L, 2L, 27L, 2L, 27L, 26L,
27L, 6L, 2L, 26L, 13L, 6L, 2L, 23L, 23L, 4L, 26L, 28L, 3L, 28L,
22L, 26L, 1L, 6L, 26L, 27L, 6L, 27L, 2L, 6L, 21L, 4L, 6L, 28L,
28L, 4L, 4L, 2L, 28L, 2L, 21L, 2L, 2L, 7L, 23L, 27L, 4L, NA,
27L, 22L, 4L, 6L, 26L, 26L, 6L, 27L, 27L, 7L, 1L, 4L, 26L, 6L,
4L, 13L, NA, 24L, 2L, 2L, 2L, 2L, 4L, 6L, 28L, 3L, 3L, 6L, NA,
1L, 2L, 1L, 2L, 2L, 3L, 25L, 23L, 2L, 4L, 28L, 28L, 3L, 28L,
28L, 2L, 2L, 28L, 28L, 6L, 1L, 28L, 2L, 2L, 4L, 26L, 6L, 2L,
28L, 2L, 25L, 2L, 7L, 6L, 27L, 28L, NA, NA, 27L, 28L, 6L, 4L,
28L, 26L, 6L, 13L, 1L, 2L, 4L, 4L, 28L, 6L, 2L, 23L, 26L, 2L,
7L, 2L, 4L, NA, 2L, 27L, 7L, 26L, 2L, 13L, 1L, 6L, 27L, 23L,
26L, 22L, 6L, 2L, 27L, 2L, 28L, 2L, 2L, 27L, 28L, 24L, 27L, 6L,
27L, NA, 27L, 6L, 6L, 26L, 2L, 28L, 7L, NA, 26L, 26L, 7L, 6L,
24L, 2L, 6L, 23L, 26L, 27L, 26L, 28L, 28L, 26L, 2L, 23L, 6L,
6L, 27L, 25L, 21L, 6L, NA, 2L, 28L, 4L, 27L, 23L, 1L, 23L, 6L,
13L, 7L, 2L, 26L, 28L, 28L, 24L, 6L, 2L, 25L, 26L, 4L, 26L, 26L,
23L, 1L, 28L, 4L, 2L, 27L, 7L, 28L, 4L, 27L, NA, 6L, 7L, 15L,
26L, 28L, 15L, 26L, 24L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 26L, 1L,
2L, 1L, 2L, 1L, 2L, 2L, 23L, 28L, 3L, 2L, 15L, 24L, 28L, 15L,
2L, 15L, 26L, 15L, 28L, 13L, 2L, NA, 28L, 2L, 25L, 27L, 28L,
2L, 2L, 28L, 3L, NA, 2L, 28L, 15L, 27L, 24L, 3L, 13L, 2L, 2L,
15L, 28L, 15L, 28L, 28L, 1L, 24L, 2L, 13L, 28L, 28L, 1L, 15L,
NA, 28L, 2L, 1L, 3L, 13L, 15L, 26L, 26L, 27L, 2L, 6L, 1L, 26L,
26L, 2L, 28L, 23L, 13L, 1L, 27L, 2L, 15L, 2L, 2L, NA, 1L, 22L,
2L, NA, 1L, 1L, 23L, 2L, 26L, 24L, 28L, 27L, 2L, 26L, NA, 2L,
27L, 2L, 2L, 2L, 2L, 13L, 25L, 2L, 2L, NA, 26L, 24L, 23L, 2L,
2L, 26L, 24L, 28L, 2L, 1L, 21L, 1L, 26L, 1L, 2L, 15L, 2L, 28L,
26L, 27L, 1L, 1L, 1L, 27L, 24L, 28L, 24L, 1L, 24L, 26L, 1L, 2L,
15L, 2L, 23L, 27L, 26L, 1L, 24L, 24L, 27L, 1L, 26L, 26L, 1L,
2L, 21L, 28L, 1L, 2L, 27L, 1L, NA, 3L, 1L, 27L, 2L, 15L, 15L,
2L, 13L, 26L, 1L, 1L, NA, 28L, 13L, 15L, 26L, 1L, 3L, 1L, 3L,
2L, NA, 26L, 15L, 26L, 2L, 13L, 2L, 27L, 1L, 2L, 3L, 25L, 1L,
25L, 26L, 15L, 15L, 1L, 15L, 15L, 15L, 26L, 2L, 2L, 27L, 3L,
2L, 23L, 24L, 15L, NA, 2L, 15L, 28L, 15L, 28L, 28L, 2L, 23L,
13L, 24L, 2L, 27L, 2L, 3L, 2L, 28L, NA, 3L, 1L, 15L, 2L, 28L,
1L, 1L, 3L, 2L, 2L, 27L, 13L, 26L, 26L, 15L, 1L, 28L, 1L, 2L,
26L, 2L, 25L, 26L, 2L, 1L, 2L, 13L, NA, 28L, 1L, 13L, 28L, 1L,
13L, 23L, 23L, 1L, 2L, NA, 28L, 3L, 28L, 13L, 28L, 26L, 15L,
3L, 28L, 9L, 26L, 28L, 23L, 2L, 1L, 13L, 2L, 28L, 1L, 15L, 1L,
28L, 1L, 25L, 2L, NA, 26L, 2L, 2L, 26L, 28L, 2L, 23L, 13L, 28L,
28L, 1L, 3L, 26L, 3L, 28L, 25L, 21L, 1L, 1L, 28L, 28L, 26L, 2L,
27L, 1L, 21L, NA, 26L, 28L, 15L, 2L, 2L, 13L, 2L, 28L, 2L, 3L,
26L, 3L, 23L, 27L, 3L, 23L, 2L, 2L, 26L, 2L, 24L, 1L, 1L, 1L,
2L, 25L, 26L, 24L, 1L, 1L, 25L, 21L, 13L, 27L, 2L, 24L, 24L,
2L, 2L, 2L, 26L, 13L, 27L, 26L, 28L, 2L, 2L, 2L, 26L, 28L, 25L,
21L, 1L, 1L, 26L, 2L, 1L, 1L, 2L, 27L, 24L, 28L, 26L, 1L, 26L,
24L, 2L, 28L, 26L, 1L, 28L, 28L, 26L, 26L, 2L, 24L, 24L, 21L,
2L, 3L, 25L, 28L, 2L, 1L, 2L, 27L, 24L, 28L, NA, 26L, 24L, 1L,
2L, 2L, 26L, 1L, 26L, 1L, 2L, 1L, 1L, 2L, 27L, 1L, 2L, 27L, 26L,
1L, 26L, 1L, 2L, 1L, 28L, 1L, 3L, 26L, 2L, 26L, 26L, NA, 1L,
28L, 26L, 24L, 1L, 26L, 1L, 1L, 1L, 1L, 24L, 26L, 2L, 28L, 1L,
28L, 3L, 26L, 2L, 27L, 24L, 2L, 25L, 1L, 2L, 23L, 2L, 28L, 26L,
23L, 2L, 2L, 2L, 2L, 28L, 2L, 24L, 1L, 26L, 26L, 2L, 2L, 23L,
24L, 27L, 21L, 2L, 2L, 27L, 24L, 23L, 24L, 2L, 27L, 26L, 28L,
13L, 27L, 26L, 2L, 2L, 24L, 27L, 23L, 1L, 24L, 2L, 13L, NA, 2L,
26L, 25L, 2L, 26L, 28L, 2L, 24L, 28L, 1L, 2L, 23L, 15L, 26L,
2L, 3L, 26L, 2L, 15L, 2L, 1L, 1L, 21L, 2L, 1L, 2L, 13L, 1L, 2L,
13L, 26L, 1L, 27L, 23L, 1L, 2L, 1L, 2L, 28L, 15L, 13L, 1L, 1L,
28L, 13L, 1L, 2L, 15L, 27L, 2L, 28L, 26L, 26L, 28L, 1L, 24L,
28L, 2L, 15L, 13L, 15L, 28L, 26L, 23L, 2L, 2L, 3L, 2L, 23L, 2L,
23L, 15L, 13L, 21L, 15L, 23L, 1L, 27L, 27L, 2L, 21L, 15L, 15L,
1L, 2L, 15L, 1L, 2L, 15L, 2L, 3L, 2L, 2L, 15L, 26L, 23L, 2L,
15L, 2L, 2L, 2L, 15L, 15L, 1L, 2L, 15L, 26L, 3L, 15L, 1L, 2L,
2L, 26L, 1L, 24L, 3L, 26L, 2L, 2L, 2L, 15L, 2L, 26L, 15L, 26L,
2L, 26L, 22L, NA, 1L, 27L, 2L, 2L, 26L, 28L, 13L, 3L, 1L, 1L,
1L, 15L, NA, 2L, 24L, 26L, 22L, 28L, 1L, 13L, 13L, NA, 3L, 8L,
21L, 1L, 1L, 15L, 15L, 1L, 26L, NA, 2L, 28L, 3L, 28L, NA, 23L,
26L, 15L, 15L, 2L, NA, 15L, 1L, 3L, 15L, 26L, 13L, 2L, 2L, 1L,
2L, 15L, 1L, 2L, 24L, 2L, 24L, 25L, 3L, 28L, 2L, 15L, 1L, NA,
28L, 3L, 1L, 28L, 2L, 26L, 2L, 2L, 1L, 22L, 15L, 3L, 24L, 26L,
2L, 24L, 1L, 24L, 3L, 13L, 28L, 1L, 2L, 13L, 1L, 2L, 28L, 24L,
2L, 1L, 2L, 1L, 1L, 15L, 26L, 2L, 2L, 3L, 28L, 2L, 22L, NA, 1L,
2L, 26L, 3L, 1L, 1L, 2L, 1L, 26L, 26L, 26L, 2L, 15L, 2L, 3L,
28L, 1L, 2L, 2L, 2L, NA, 26L, NA, 15L, 24L, NA, 1L, 27L, 2L,
2L, 27L, 27L, 2L, 23L, 2L, 28L, 13L, 2L, 28L, 2L, 2L, 1L, 28L,
27L, 2L, 27L, 15L, 13L, 26L, 15L, 15L, 1L, 26L, 1L, NA, 26L,
28L, 1L, 1L, 1L, 24L, NA, 28L, 1L, 1L, 2L, 15L, 25L, 2L, 1L,
15L, 27L, 3L, 1L, NA, NA, 26L, 1L, 13L, 26L, NA, NA, 2L, 23L,
27L, 24L, 26L, 28L, 25L, 3L, 26L, 1L, 1L, 28L, 1L, 2L, 24L, 2L,
1L, 2L, 2L, 15L, 1L, 1L, 15L, 28L, 15L, 15L, 26L, 22L, 28L, 2L,
28L, 1L, 24L, 28L, 28L, 15L, 15L, 3L, 15L, 24L, 26L, 28L, 1L,
24L, 27L, 3L, 1L, 3L, 2L, 13L, 1L, 28L, 13L, NA, 1L, 1L, 28L,
28L, 2L, 26L, 13L, 28L, 24L, 2L, 13L, 15L, NA, 1L, 13L, 1L, 1L,
2L, 2L, 1L, 27L, 2L, 2L, 26L, 2L, 15L, 1L, 26L, 24L, 1L, 2L,
13L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 28L, 28L, 1L, 1L, 15L, 13L,
26L, 1L, 28L, 1L, NA, 2L, 26L, 3L, 23L, 2L, 2L, NA, 1L, 28L,
2L, 3L, 2L, 1L, 2L, 1L, 2L, NA, 15L, 2L, NA, 23L, 3L, 15L, 2L,
28L, 23L, 13L, 2L, 2L, 1L, 3L, 1L, 2L, 28L, NA, 2L, 28L, 22L,
13L, 2L, 27L, 26L, 15L, 1L, 2L, 13L, 1L, 2L, 13L, 15L, 15L, 26L,
15L, 13L, 2L, 3L, 24L, 23L, 13L, 15L, 26L, 1L, 26L, 21L, 23L,
2L, 13L, 26L, 2L, 26L, 3L, 13L, 28L, 1L, 13L, 26L, 2L, 2L, 26L,
2L, 27L, 1L, 15L, 1L, 28L, 27L, 13L, 28L, 28L, 28L, 26L, NA,
21L, 3L, 27L, 15L, 1L, 28L, 28L, 23L, 3L, 23L, 3L, 24L, 9L, 2L,
9L, 23L, 27L, 26L, 24L, 26L, 2L, 28L, NA, 26L, NA, 2L, 28L, 2L,
24L, 28L, 9L, 27L, 24L, 6L, 13L, NA, 2L, 24L, 24L, 22L, 28L,
9L, 24L, NA, 24L, 24L, 2L, 24L, 23L, 28L, 28L, 26L, 26L, 2L,
8L, 1L, 26L, 22L, 2L, 23L, 1L, 27L, 22L, 2L, 26L, 15L, 2L, 24L,
2L, 2L, 2L, 15L, 26L, 26L, 8L, 9L, 2L, 8L, 2L, 26L, 2L, 8L, 15L,
25L, 8L, 15L, 15L, 2L, 1L, 8L, 9L, NA, 23L, 26L, 9L, 9L, 26L,
9L, 26L, 9L, 13L, 8L, 25L, 8L, 1L, 28L, 23L, 9L, 2L, 27L, 8L,
28L, 15L, 9L, 24L, 9L, 28L, 26L, 8L, 9L, 2L, 9L, 8L, 3L, 28L,
15L, 8L, 15L, NA, 2L, 27L, 2L, NA, 28L, 28L, 8L, 8L, 1L, 15L,
8L, 2L, 2L, 8L, 26L, NA, NA, 27L, 28L, 9L, 26L, 15L, 6L, 2L,
2L, 2L, NA, 26L, 26L, 8L, 15L, NA, 26L, 8L, 27L, 9L, 27L, 28L,
26L, 28L, 8L, 15L, 8L, 8L, 3L, 15L, NA, 9L, 8L, 9L, 15L, 9L,
28L, 28L, 9L, 9L, 26L, 28L, 28L, 2L, 2L, 26L, 2L, 1L, 1L, 2L,
1L, 2L, 23L, 25L, 3L, 2L, 15L, 28L, 26L, 1L, 28L, 26L, 23L, 2L,
1L, 25L, 26L, 24L), levels = c("PP ", "PSOE ", "Ciudadanos ",
"En Com├║ Podem ", "M├®s Comprom├¡s", "ERC ", "JxCat ",
"EAJ-PNV", "EH Bildu ", "CCa-NC ", "Na+ ", "PACMA ",
"VOX ", "CUP ", "Unidas Podemos ", "BNG ", "PRC ",
"Más País ", "En Común-Unidas Podemos ",
"Teruel Existe ", "Nulo ", "No tenía derecho a voto ",
"No tenía edad ", "Otros partidos ", "En blanco ", "No votó ",
"No recuerda ", "N.C. "), class = "factor"), CIS.P3 = structure(c(3L,
3L, 3L, 3L, 3L, 2L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 2L,
2L, 3L, 3L, 2L, 1L, 2L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L,
1L, 2L, 3L, 1L, 2L, 2L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 3L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 2L, 1L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L,
3L, 2L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 1L,
2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 2L, 1L, 3L, 1L,
3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L,
3L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L,
1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L,
3L, 3L, 1L, 3L, 2L, 3L,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 2L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L), levels = c("Mejor", "(NO LEER) Igual", "Peor", "N.S.",
"N.C."), class = "factor")), class = "data.frame", row.names = c(NA,
-2787L))

The following code does the job:
library(tidyverse)
library(haven)
library(labelled)
CIS = read_spss("CISData.sav") %>%
mutate(
P3 = to_factor(P3),
RECUERDO = to_factor(RECUERDO)) %>%
mutate(P3 = sub("\\(NO LEER\\) ", "", P3))
include = c("PSOE", "PP", "VOX", "Unidas Podemos", "Ciudadanos")
CISb = CIS %>%
filter(RECUERDO %in% include) %>%
group_by(RECUERDO, P3) %>%
tally() %>%
mutate(pct = n/sum(n))
plot2 = ggplot(CISb, aes(x = pct, y = RECUERDO)) +
geom_bar(stat = "identity") +
facet_wrap(~P3, scales = "free_x") +
theme_bw() +
labs(x = "Percentage of Party's Voters in Each Response", y = "Voting Preference in 2019 General Elections",
title = "Current VS Past Individual Situation Based on Voting Preference")
plot2

Related

ggalluvial assign different color for each node

I was following this post, but I do not get how can I manage it with my data.
My plot looks like:
And I would like that the "strings" were the same color as the 2nd column, i.e. for ESR1 I would like the orange string, and for PIK3CA green.
Any idea about how can I manage with scale_fill_manual or any other argument?
Thanks!
My code:
colorfill <- c("white", "white", "darkgreen", "orange", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white")
ggplot(data = Allu,
aes(axis1 = Gene_mut, axis2 = Metastasis_Location, y = Freq)) +
geom_alluvium(aes(fill = Gene_mut),
curve_type = "quintic") +
geom_stratum(width = 1/4, fill = colorfill) +
geom_text(stat = "stratum", size = 3,
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Metastasis_Location", "Gene_mut"),
expand = c(0.05, .05)) +
theme_void()
My data:
structure(list(Metastasis_Location = structure(c(1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L), .Label = c("adrenal", "bone", "breast", "liver", "lung",
"muscle", "node", "pancreatic", "peritoneum", "pleural", "skin"
), class = "factor"), T0_T2_THERAPY_COD = structure(c(2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,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4L,
4L, 4L, 5L, 5L, 5L, 6L, 3L, 6L, 6L, 6L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 6L, 2L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 5L, 6L, 3L, 6L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 3L, 4L, 4L, 5L, 6L,
1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 3L, 4L, 3L, 4L, 5L, 6L, 3L, 3L, 4L, 5L, 6L, 6L, 6L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 3L, 4L, 3L, 4L, 5L,
6L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 1L, 6L, 3L, 3L, 4L, 4L, 5L
), .Label = c("AKT1", "ERBB2", "ESR1", "PIK3CA", "TP53", "WT"
), class = "factor"), LABO_ID = structure(c(45L, 8L, 13L, 11L,
11L, 26L, 7L, 15L, 23L, 26L, 35L, 39L, 7L, 19L, 26L, 32L, 33L,
35L, 39L, 15L, 19L, 35L, 1L, 37L, 34L, 43L, 47L, 3L, 10L, 18L,
20L, 28L, 31L, 36L, 42L, 9L, 10L, 14L, 18L, 20L, 28L, 31L, 36L,
44L, 45L, 8L, 10L, 18L, 28L, 42L, 2L, 7L, 39L, 7L, 39L, 3L, 4L,
42L, 5L, 42L, 6L, 21L, 1L, 10L, 22L, 28L, 46L, 9L, 10L, 14L,
28L, 46L, 10L, 28L, 48L, 25L, 23L, 32L, 33L, 40L, 43L, 24L, 3L,
18L, 24L, 28L, 31L, 36L, 42L, 18L, 27L, 28L, 31L, 36L, 45L, 18L,
24L, 27L, 28L, 42L, 16L, 16L, 18L, 18L, 18L, 29L, 23L, 39L, 39L,
40L, 1L, 12L, 47L, 3L, 18L, 20L, 28L, 31L, 36L, 38L, 42L, 5L,
18L, 20L, 27L, 28L, 31L, 36L, 38L, 41L, 45L, 8L, 18L, 27L, 28L,
42L, 48L, 6L, 17L, 30L, 31L, 31L, 18L, 18L, 18L, 29L, 39L, 39L,
40L, 43L, 31L, 31L, 48L, 30L, 13L, 34L, 18L, 36L, 18L, 36L, 18L
), .Label = c("ER-11", "ER-19", "ER-21", "ER-22", "ER-29", "ER-30",
"ER-31", "ER-32", "ER-33", "ER-38", "ER-40", "ER-43", "ER-49",
"ER-8", "ER-AZ-04", "ER-AZ-05", "ER-AZ-06", "ER-AZ-07", "ER-AZ-08",
"ER-AZ-10", "ER-AZ-11", "ER-AZ-11=ER-47", "ER-AZ-13", "ER-AZ-14",
"ER-AZ-15", "ER-AZ-16", "ER-AZ-17", "ER-AZ-18", "ER-AZ-20", "ER-AZ-20=ER-27",
"ER-AZ-21", "ER-AZ-23", "ER-AZ-23=ER-52", "ER-AZ-24", "ER-AZ-29",
"ER-AZ-31", "ER-AZ-33", "ER-AZ-35", "ER-AZ-37", "ER-AZ-38", "ER-AZ-39",
"ER-AZ-40", "ER-AZ-43", "ER-AZ-44", "ER-AZ-45", "ER-AZ-49", "ER-AZ-51",
"ER-AZ-53"), class = "factor"), Freq = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -161L), groups = structure(list(
Metastasis_Location = structure(c(1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L), .Label = c("adrenal",
"bone", "breast", "liver", "lung", "muscle", "node", "pancreatic",
"peritoneum", "pleural", "skin"), class = "factor"), T0_T2_THERAPY_COD = structure(c(2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("A",
"F"), class = "factor"), T0_T2_PD_event = structure(c(2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L), .Label = c("No Progression",
"Progression"), class = "factor"), Gene_mut = structure(c(4L,
5L, 1L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 6L, 3L, 6L, 3L, 4L, 5L,
6L, 2L, 3L, 4L, 3L, 4L, 5L, 6L, 3L, 6L, 3L, 4L, 5L, 6L, 3L,
4L, 5L, 6L, 1L, 3L, 4L, 5L, 3L, 4L, 3L, 4L, 5L, 6L, 3L, 4L,
5L, 6L, 6L, 3L, 4L, 5L, 6L, 3L, 4L, 3L, 4L, 5L, 6L, 3L, 4L,
5L, 6L, 3L, 4L, 5L, 6L, 1L, 6L, 3L, 4L, 5L), .Label = c("AKT1",
"ERBB2", "ESR1", "PIK3CA", "TP53", "WT"), class = "factor"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8:12,
13:19, 20:22, 23L, 24L, 25:27, 28:35, 36:45, 46:50, 51L,
52L, 53L, 54:55, 56:58, 59L, 60L, 61L, 62L, 63L, 64:67,
68:72, 73:75, 76L, 77L, 78:79, 80L, 81L, 82L, 83:89,
90:95, 96:100, 101L, 102L, 103L, 104L, 105L, 106L, 107:108,
109L, 110L, 111:112, 113L, 114:121, 122:131, 132:137,
138:140, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L,
149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157:158,
159:160, 161L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -72L), .drop = TRUE))
You're right to think of scale_fill_manual(). I think this is the more programmable alternative to passing a vector like colorfill to an aesthetic outside aes(). The following plot uses your data and color vector to control how the fill aesthetic is coded throughout the plot, and notice that fill is passed the same variable, Gene_mut, in both layers (alluvium and stratum):
ggplot(data = Allu,
aes(axis1 = Gene_mut, axis2 = Metastasis_Location, y = Freq)) +
geom_alluvium(aes(fill = Gene_mut),
curve_type = "quintic") +
geom_stratum(aes(fill = Gene_mut), width = 1/4) +
scale_fill_manual(values = colorfill) +
geom_text(stat = "stratum", size = 3,
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Metastasis_Location", "Gene_mut"),
expand = c(0.05, .05)) +
theme_void()
Since Metastasis_Location takes different values than Gene_mut, fill treats those strata as having missing values, which by default are colored grey. You can change that behavior by passing a color string to the na.value parameter of scale_fill_manual().

Group columns and fit in one plot

I have the following data-frame:
structure(list(inst = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A8",
"b7", "X1"), class = "factor"), steps = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("5",
"10", "20"), class = "factor"), family = structure(c(6L,
1L, 4L, 5L, 7L, 2L, 3L, 6L, 7L, 5L, 4L, 1L, 2L, 3L, 3L, 6L, 4L,
1L, 2L, 7L, 5L, 6L, 5L, 4L, 1L, 7L, 2L, 3L, 6L, 5L, 4L, 7L, 1L,
2L, 3L, 6L, 1L, 3L, 5L, 4L, 2L, 7L, 6L, 4L, 1L, 5L, 7L, 2L, 3L,
6L, 4L, 1L, 5L, 7L, 2L, 3L, 6L, 3L, 5L, 4L, 7L, 1L, 2L), .Label = c("Bay",
"Grad", "none", "Upp", "base", "new", "tuna"
), class = "factor"), mean_error = c(5.930259, 6.0611, 6.241703,
6.270109, 6.277435, 6.587473, 6.865757, 5.936106, 6.084044, 6.140153,
6.142072, 6.146425, 6.364658, 6.621481, 6.759502, 7.02175, 7.16422,
7.19518, 7.36932, 7.395606, 7.44191, 5.113961, 5.123312, 5.289946,
5.292267, 5.455671, 5.768393, 5.840368, 5.140513, 5.346728, 5.371491,
5.463127, 5.475944, 5.602034, 5.995647, 5.784786, 6.00454, 6.121524,
6.22509, 6.24901, 6.37396, 6.41903, 4.0439, 4.223119, 4.260518,
4.31062, 4.500065, 4.822419, 5.107085, 4.221596, 4.371242, 4.505292,
4.524415, 4.681877, 4.703846, 5.14499, 4.944005, 5.007325, 5.0561975,
5.1926225, 5.3353825, 5.34204, 5.63557)), row.names = c(64L,
3L, 38L, 55L, 73L, 12L, 21L, 67L, 76L, 58L, 41L, 6L, 15L, 24L,
27L, 70L, 44L, 9L, 18L, 79L, 61L, 63L, 54L, 37L, 2L, 72L, 11L,
20L, 66L, 57L, 40L, 75L, 5L, 14L, 23L, 69L, 8L, 26L, 60L, 43L,
17L, 78L, 62L, 36L, 1L, 53L, 71L, 10L, 19L, 65L, 39L, 4L, 56L,
74L, 13L, 22L, 68L, 25L, 59L, 42L, 77L, 7L, 16L), class = "data.frame")
I am trying to create groups of three steps per inst in the x-axis and fit everything in one plot. The outcome should resemble this
So far I tried:
df_bri %>% select(steps, inst, family, mean_error) %>%
ggplot(aes(x = steps, y = mean_error, fill = mean_error)) +
geom_boxplot()
and I get this:
I don't know how to separate the groups into 3 steps per inst.
fill=steps is what you need:
ggplot(df, aes(x = inst, y = mean_error, fill = steps)) +
geom_boxplot()

How to create histograms for each unique combination of levels from two factors?

I cannot figure out how to use a loop to plot one histogram for each unique combination of levels from TWO factors.
Here is my data: https://www.dropbox.com/sh/exsjhu23fnpwf4r/AABvitLBN1nRMpXcyYMVIOIDa?dl=0
# perhaps need to have factors
df$freq <- as.factor(df$freq)
df$time <- as.factor(df$time)
I learned how to use a loop to plot histograms for ONE factor levels:
# space for plots
windows(width=19, height=10)
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0),
oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21), nrow=3, ncol=7, byrow=T))
layout.show(a)
# loop
for (i in 1:length(unique(df$freq))) {
value <- subset(df, freq == unique (df$freq)[i])
hist(value$thr, main=paste0("freq: ", unique(df$freq)[i]))
}
I tried variations of this loop for TWO factors but that unfortunately does not work:
for (i in 1:length(unique(df[c("freq", "time")]))) {
value <- subset(df, freq == unique (df$freq)[i] & time == unique(df$time)[i])
hist(value$thr, main=paste0("freq: ", unique(df$freq)[i]))
}
I would also like to learn how to label each histogram based on the levels of TWO factors (not just one)...
It's more convenient to use by here.
For the titles we prefer characters to factors.
df1[c("freq", "time")] <- lapply(df1[c("freq", "time")], as.character)
Then open windows,
windows(width=19, height=10)
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0),
oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(1:21, 3, 7))
layout.show(a)
and plot.
by(df1, df1[c("freq", "time")], function(x)
hist(x$thr, main=paste("freq:", paste(x[1, c(1, 3)], collapse=","))))
Result
Edit
To get the specific order we probably have to do some more stuff.
df1[c("freq", "time")] <- lapply(df1[c("freq", "time")], as.character)
windows(width=19, height=10)
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0),
oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(1:21, 3, 7, byrow=TRUE)) # with byrow
layout.show(a)
l <- split(df1, df1[c("freq", "time")])
m <- t(sapply(l, function(x) x[1, c(1, 3)])) # matrix of first rows of each subset
m[, 2] <- sub("m", "", m[, 2]) # use the values...
m <- apply(m, 1:2, as.numeric) # ... make numeric
Now we obtain the histograms within a lapply over the list ordered by m.
lapply(l[order(m[, 2], m[, 1])], function(x)
hist(x$thr, main=paste("freq:", paste(x[1, c(1, 3)], collapse=","))))
New Result
Data
df1 <- structure(list(freq = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L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 10L, 10L, 10L,
20L, 25L, 45L, 20L, 10L, 10L, 10L, 20L, 25L, 45L, 15L, 10L, 10L,
10L, 20L, 30L, 60L, 30L, 10L, 10L, 10L, 15L, 30L, 50L, 25L, 10L,
10L, 10L, 20L, 30L, 45L, 25L, 15L, 10L, 15L, 30L, 35L, 50L, 25L,
15L, 10L, 15L, 25L, 35L, 60L, 25L, 10L, 10L, 15L, 20L, 30L, 60L,
25L, 5L, 5L, 10L, 20L, 30L, 45L, 20L, 5L, 10L, 10L, 20L, 30L,
45L, 20L, 10L, 10L, 10L, 20L, 30L, 60L, 30L, 15L, 10L, 15L, 25L,
30L, 55L, 25L, 10L, 10L, 10L, 20L, 30L, 55L, 35L, 10L, 10L, 10L,
20L, 30L, 60L, 35L, 15L, 10L, 10L, 15L, 25L, 50L, 30L, 10L, 10L,
10L, 20L, 25L, 55L, 25L, 10L, 10L, 15L, 25L, 25L, 65L, 30L, 10L,
10L, 15L, 20L, 30L, 60L, 30L, 15L, 15L, 15L, 15L, 30L, 55L, 35L,
15L, 15L, 15L, 25L, 35L, 55L, 35L, 15L, 15L, 15L, 25L, 35L, 60L,
35L, 15L, 15L, 15L, 25L, 35L, 60L, 30L, 10L, 10L, 15L, 25L, 35L,
55L, 30L, 15L, 10L, 10L, 25L, 30L, 50L, 25L, 10L, 10L, 10L, 20L,
30L, 55L, 30L, 10L, 10L, 15L, 20L, 30L, 55L, 30L, 10L, 15L, 20L,
25L, 35L, 55L, 25L, 15L, 15L, 15L, 25L, 40L, 50L, 20L, 10L, 10L,
20L, 30L, 40L, 45L, 25L, 10L, 10L, 10L, 20L, 30L, 50L, 25L, 10L,
10L, 10L, 20L, 25L, 55L, 20L, 10L, 10L, 15L, 25L, 35L, 50L, 20L,
10L, 10L, 15L, 25L, 30L, 45L, 20L, 15L, 10L, 10L, 20L, 30L, 50L,
20L, 15L, 15L, 15L, 20L, 30L, 60L, 35L, 15L, 10L, 15L, 25L, 30L,
60L, 35L, 15L, 15L, 15L, 30L,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L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("3m", "6m", "9m"), class = "factor")), row.names = c(NA,
-322L), class = "data.frame")

ggplot troubleshoot: Error: Aesthetics must be either length 1 or the same as the data (24): x, y, fill

Sample data:
> dput(droplevels(data[1:50, ]))
structure(list(QtySold = c(3L, -1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 6L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 4L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L,
2L, -1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L,
1L, 1L, 4L, 5L, 1L, 3L, 1L, 2L, 30L, 2L, 10L, 6L, 10L, 8L, 10L,
20L, 10L, 5L, 1L, 3L, 30L, 20L, 10L, 10L, 90L, 10L, 12L, 10L,
15L, 10L, 10L, 20L, 10L, 10L, 32L, 10L, 10L, 20L, 1L, 2L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L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Handpieces",
"PRIVATE LABEL", "SUNDRY"), class = "factor"), MAJOR_CATEGORY_KEY = structure(c(23L,
23L, 23L, 23L, 21L, 21L, 21L, 23L, 23L, 23L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 25L, 19L, 19L, 4L, 4L, 9L, 9L,
9L, 9L, 9L, 9L, 23L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 17L, 16L, 4L, 4L, 4L, 4L, 11L, 11L, 4L, 4L, 4L,
4L, 11L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 21L, 12L, 12L, 12L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 13L, 13L, 7L, 18L, 7L,
19L, 19L, 19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 13L, 13L, 11L, 11L, 6L, 12L, 10L, 10L,
4L, 4L, 4L, 4L, 4L, 4L, 10L, 19L, 19L, 19L, 19L, 19L, 4L, 4L,
11L, 11L, 11L, 11L, 11L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 11L, 16L, 16L, 16L, 16L, 16L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 16L, 16L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 21L, 4L,
4L, 4L, 19L, 19L, 19L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 17L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 12L, 12L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 13L,
4L, 4L, 4L, 4L, 4L, 4L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 18L,
21L, 21L, 18L, 18L, 18L, 18L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 19L, 19L, 18L, 18L, 4L, 11L), .Label = c("AIR ",
"AML ", "ANS ", "ASE ", "ASP ", "B&D ", "BLE ", "C&P ", "CBL ",
"CEM ", "CMP ", "CRN ", "END ", "FNP ", "GYP ", "HND ", "IMP ",
"INS ", "OTH ", "P&P ", "PRE ", "RTC ", "SME ", "UCL ", "XRY "
), class = "factor")), row.names = c(NA, 500L), class = "data.frame")
And the relevant code
> newdf = subset(data, select = c(QtySold, PRODUCT_SUB_LINE_DESCR, MAJOR_CATEGORY_KEY))
> sample = newdf %>%
+ filter(QtySold < 0) %>%
+ group_by(PRODUCT_SUB_LINE_DESCR,MAJOR_CATEGORY_KEY) %>%
+ summarise(returns = sum(QtySold)) %>%
+ spread(PRODUCT_SUB_LINE_DESCR,returns, fill=0) %>%
+ mutate(total_returns = `PRIVATE LABEL` + SUNDRY + Handpieces,
+ PercentageReturn_PL = round(`PRIVATE LABEL`/ total_returns*100,1))
> sample %>%
+ group_by(PRODUCT_SUB_LINE_DESCR) %>%
+ summarise(count=n()) %>%
+ mutate(percent= paste0(round(count/sum(count)*100,1),'%'))
Error in grouped_df_impl(data, unname(vars), drop) :
Column `PRODUCT_SUB_LINE_DESCR` is unknown
My scenario:
List item
I have a data set of three products: Sundry, Handpieces and Private Label.
I filtered the data set based on (QtySold < 0) category and group the new one based on "PRODUCT_SUB_LINE_DESCR" and
"MAJOR_CATEGORY_KEY".
I want to see the percentage of returned items which belong to Private Label category. My code has a mutate command with a new column "PercentageReturn_PL".
Finally, when I am done with the math, I want to plot this whole thing with the MAJOR_CATEGORY_KEY on the Y-axis and the stacked barplot show the segments of percentage break down for three products within one MAJOR_CATEGORY_KEY.
How can I fix this error?
> sample %>%
+ group_by(PRODUCT_SUB_LINE_DESCR) %>%
+ summarise(count=n()) %>%
+ mutate(percent= paste0(round(count/sum(count)*100,1),'%'))
Error in grouped_df_impl(data, unname(vars), drop) :
Column `PRODUCT_SUB_LINE_DESCR` is unknown
The problem was that you spread out a column and then tried to use it. Also a lot of extra stuff going on in the graph.
Here's a way to get the percent of returns that are PRIVATE LABEL by major category:
sample_long =
newdf %>%
filter(QtySold < 0) %>%
group_by(MAJOR_CATEGORY_KEY) %>%
summarize(returns = sum(QtySold),
percent_returns_pl = sum(QtySold[PRODUCT_SUB_LINE_DESCR == "PRIVATE LABEL"]) / returns)
And then we can plot easily:
ggplot(
sample_long,
aes(x = MAJOR_CATEGORY_KEY, y = percent_returns_pl)
) +
geom_col() +
geom_text(aes(label = scales::percent(percent_returns_pl)), vjust = -0.5) +
scale_y_continuous(labels = scales::percent)
It's a boring plot with sample data, should be much more interesting with the full data.

reshape/remould data frame to create normalized bar chart and pie chart

I have the following data_frame structure which has been read from a csv file (appended). Basically, this summarises for each Operator (A M D L J) whether their score is Excellent, Good, Ok, Poor or Terrible. The other fields date and scorer ( I plan to use later but are not required at the moment).
What I am struggling with is how to reduce this data to a format that allows me to plot a bar chart (normalized by dividing total counts for each operator) and a bar chart. How do I reduce this data frame to something like the following which will allow me to greate geom_bar.
Operator Score Count
A Good 11
A Poor 5
A Ok 3
A Terrible 0
A Excellent 0
D Good 36
D Poor 50
D Ok 10
D Terrible 1
D Excellent 0
I know I can subset the initial data frame according to operator and then get the numbers from summary
dfA = subset(df, Operator=='A')
summary(dfA)
but I would like to automate this process (i.e automatically remould the data frame into the above structure from which I can use ggplot2 to visualise the results). However, I have no idea where to start with this problem
structure(list(Operator = structure(c(5L, 5L, 5L, 5L, 5L, 5L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L,
3L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 5L, 2L, 2L, 2L,
2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 5L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 5L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 4L, 3L, 3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L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 1L, 3L, 1L, 1L, 2L,
3L, 1L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 1L,
2L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 2L,
2L, 3L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 2L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L), .Label = c("Good",
"OK", "Poor", "Terrible"), class = "factor"), Date = structure(c(3L,
3L, 5L, 5L, 5L, 7L, 3L, 3L, 9L, 9L, 9L, 11L, 11L, 3L, 3L,
5L, 5L, 5L, 7L, 7L, 7L, 11L, 11L, 11L, 3L, 15L, 15L, 21L,
13L, 17L, 17L, 19L, 21L, 13L, 13L, 13L, 15L, 15L, 17L, 17L,
17L, 19L, 19L, 19L, 21L, 21L, 30L, 30L, 23L, 25L, 25L, 25L,
27L, 27L, 27L, 29L, 29L, 29L, 23L, 23L, 25L, 25L, 25L, 27L,
27L, 27L, 30L, 30L, 30L, 30L, 30L, 32L, 32L, 36L, 2L, 36L,
36L, 36L, 39L, 39L, 34L, 34L, 34L, 36L, 36L, 36L, 39L, 39L,
2L, 2L, 32L, 34L, 34L, 36L, 41L, 41L, 41L, 43L, 1L, 38L,
38L, 41L, 42L, 43L, 38L, 38L, 41L, 41L, 41L, 42L, 42L, 42L,
43L, 43L, 1L, 1L, 1L, 38L, 42L, 42L, 42L, 42L, 1L, 1L, 1L,
3L, 3L, 7L, 3L, 3L, 3L, 5L, 7L, 11L, 3L, 3L, 3L, 3L, 5L,
5L, 5L, 7L, 7L, 7L, 9L, 9L, 11L, 11L, 11L, 13L, 15L, 17L,
19L, 19L, 21L, 21L, 13L, 21L, 13L, 13L, 13L, 15L, 17L, 17L,
17L, 19L, 19L, 21L, 21L, 21L, 29L, 29L, 29L, 30L, 23L, 25L,
29L, 29L, 23L, 23L, 23L, 25L, 25L, 25L, 27L, 27L, 30L, 30L,
30L, 32L, 32L, 32L, 2L, 2L, 39L, 39L, 32L, 32L, 32L, 34L,
34L, 34L, 36L, 36L, 2L, 2L, 2L, 43L, 1L, 38L, 41L, 41L, 42L,
42L, 42L, 43L, 43L, 1L, 1L, 43L, 1L, 42L, 1L, 1L, 1L, 32L,
32L, 36L, 2L, 36L, 36L, 36L, 39L, 39L, 34L, 34L, 34L, 36L,
36L, 36L, 39L, 39L, 2L, 2L, 32L, 34L, 34L, 36L, 10L, 4L,
6L, 6L, 10L, 10L, 10L, 12L, 4L, 4L, 12L, 12L, 6L, 6L, 6L,
8L, 8L, 8L, 12L, 12L, 14L, 16L, 14L, 14L, 18L, 20L, 14L,
18L, 18L, 18L, 14L, 14L, 14L, 16L, 16L, 16L, 22L, 22L, 22L,
28L, 28L, 31L, 28L, 28L, 28L, 31L, 31L, 31L, 33L, 33L, 33L,
35L, 35L, 35L, 37L, 37L, 37L, 33L, 33L, 33L, 35L, 37L, 37L,
40L, 40L, 32L, 32L, 32L, 2L, 2L, 39L, 39L, 32L, 32L, 32L,
34L, 34L, 34L, 36L, 36L, 2L, 2L, 2L, 6L, 6L, 10L, 10L, 10L,
10L, 4L, 4L, 6L, 6L, 8L, 8L, 8L, 10L, 10L, 12L, 4L, 8L, 8L,
8L, 8L, 12L, 4L, 4L, 4L, 4L, 8L, 12L, 16L, 16L, 14L, 16L,
18L, 18L, 20L, 20L, 20L, 14L, 14L, 20L, 20L, 22L, 22L, 14L,
16L, 18L, 18L, 18L, 18L, 24L, 24L, 24L, 26L, 26L, 31L, 31L,
24L, 26L, 26L, 26L, 26L, 24L, 24L, 24L, 24L, 31L, 31L, 40L,
37L, 33L, 33L, 33L, 33L, 35L, 35L, 35L, 37L, 37L, 37L, 37L,
40L), .Label = c("01/02/2013", "01/03/2013", "04/02/2013",
"04/03/2013", "05/02/2013", "05/03/2013", "06/02/2013", "06/03/2013",
"07/02/2013", "07/03/2013", "08/02/2013", "08/03/2013", "11/02/2013",
"11/03/2013", "12/02/2013", "12/03/2013", "13/02/2013", "13/03/2013",
"14/02/2013", "14/03/2013", "15/02/2013", "15/03/2013", "18/02/2013",
"18/03/2013", "19/02/2013", "19/03/2013", "20/02/2013", "20/03/2013",
"21/02/2013", "22/02/2013", "22/03/2013", "25/02/2013", "25/03/2013",
"26/02/2013", "26/03/2013", "27/02/2013", "27/03/2013", "28/01/2013",
"28/02/2013", "28/03/2013", "29/01/2013", "30/01/2013", "31/01/2013"
), class = "factor"), Scorer = structure(c(2L, 2L, 3L, 3L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 3L, 3L, 1L,
3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 3L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L,
3L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 3L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,
2L, 2L, 1L), .Label = c("", "B", "G"), class = "factor")), .Names = c("Operator",
"ROI_Score", "Date", "Scorer"), row.names = c(NA, -412L), class = "data.frame")
Here's to prepare your data using data.table:
require(data.table)
dt <- data.table(df)
ops <- as.character(unique(dt$Operator))
scr <- as.character(unique(dt$ROI_Score))
oo <- setkey(dt[, .N, by="Operator,ROI_Score"], Operator,
ROI_Score)[CJ(ops, scr)][is.na(N), N:= 0L]
And here's how you can get a normalised bar-chart with this data:
oo[, N.norm := N/sum(N), by=Operator]
One way to plot this would be with x = Operator:
require(ggplot2)
ggplot(data = oo, aes(x = Operator, y = N.norm)) +
geom_bar(positon="stack", stat="identity", aes(fill = ROI_Score))
You can simply do something like this to prepare your data :
data.frame(table(Operator=df$Operator, Score=df$ROI_Score))
Which gives :
Operator Score Freq
1 A Good 11
2 D Good 36
3 J Good 54
4 L Good 44
5 M Good 28
6 A OK 3
7 D OK 10
8 J OK 9
9 L OK 4
10 M OK 7
11 A Poor 5
12 D Poor 50
13 J Poor 56
14 L Poor 67
15 M Poor 27
16 A Terrible 0
17 D Terrible 1
18 J Terrible 0
19 L Terrible 0
20 M Terrible 0

Resources