I am working on a small research project with basis on the information made available by CIS (a Spanish research institute), where unfortunately the data is really messy.
I have in P3 several options ("Better", "Same", "Worse", "Don't Know", "Don't Answer") on how Spanish people rate their present socioeconomic situation vs the one that was 6 months ago.
And in P20 there are contained all the ages of the respondents, from 16 to 92. Say, you have age 17 in row 1, age 28 in row 2, age 65 in row 3, age 43 in row 4, and so on (out of a total of 2787 rows).
The class of both P3 and P20 is factor.
What I am looking for is to see how I can organise the data in P20 in order to group it in intervals ("16-29", "30-44", "45-64", ">65", "Don't Answer"), so that I can plot this information later on.
So far, the code that I have tried is, as follows:
library(ggplot2)
set.seed(123)
CIS_data_5 <- data.frame(
CIS$P20,
CIS$P3
)
CIS$P3 <- factor(CIS$P3, labels = c("Better", "Same", "Worse", "Don't Know", "Don't Answer"))
CIS$P20 <- factor(CIS$P20, labels = c("16-29", "30-44", "45-64", ">65", "Don't Answer"))
ggplot(CIS, aes(P3, fill = P20)) +
geom_bar() +
scale_fill_manual(values = c("LightGreen", "Green"), guide = "none") +
facet_grid(~ P20) +
xlab("Current VS Past Individual Situation")
However, I get the following error:
Error in factor(CIS$P20, labels = c("16-29", "30-44", "45-64", ">65", :
invalid 'labels'; length 5 should be 1 or 0
I have tried different solutions to get around this issue, namely, cut and cut2 by looking at answers for similar questions; however, this has got me nowhere.
Any help on how I could possibly tackle this issue would be enormously appreciated.
Many thanks in advance!