Hello, I have a Dataframe (Df), in this case I want to explore the Sumscores of 6 Subscales. The subscales (emotionaler Missbrauch, körperlicher Missbrauch, emotionale Vernachlässigung, körperliche Vernachlässigung, sexueller Missbrauch) are assigned to the CTQ_TOTAL which is assigned to Df like this:
#Subskalen des CTQ = wenn klein geschrieben dann einfach nur die Daten, wenn groß geschrieben, dann die rowSums, wenn ms dahinter dann die means
CTQ_emo_abuse <- select(Df, CQ01_03, CQ01_08, CQ01_14, CQ01_18, CQ01_25)
CTQ_pys_abuse <- select(Df, CQ01_09, CQ01_11, CQ01_12, CQ01_15, CQ01_17)
CTQ_sex_abuse <- select(Df, CQ01_20, CQ01_21, CQ01_23, CQ01_24, CQ01_27)
CTQ_emo_negl <- select(Df, CQ01_05, CQ01_07, CQ01_13, CQ01_19, CQ01_28)
CTQ_pys_negl <- select(Df, CQ01_01,CQ01_02, CQ01_04, CQ01_06, CQ01_26)
CTQ_total <- data.frame (CTQ_emo_abuse,CTQ_emo_negl, CTQ_pys_abuse, CTQ_pys_negl, CTQ_sex_abuse)
#zuordnen der Subskalen zusätzlich in Df
Df$CTQ_emo_abuse <- CTQ_emo_abuse
Df$CTQ_pys_abuse <- CTQ_pys_abuse
Df$CTQ_sex_abuse <-CTQ_sex_abuse
Df$CTQ_emo_negl <- CTQ_emo_negl
Df$CTQ_pys_negl <- CTQ_pys_negl
Df$CTQ_total <- CTQ_total
CTQ_TOTAL <-data.frame(SEX_ABUSE = rowSums(Df$CTQ_sex_abuse),
EMO_ABUSE = rowSums(Df$CTQ_emo_abuse),
PYS_ABUSE = rowSums (Df$CTQ_pys_abuse),
EMO_NEGL = rowSums (Df$CTQ_emo_negl),
PYS_NEGL = rowSums (Df$CTQ_pys_negl))
Df$CTQ_TOTAL <-CTQ_TOTAL
The rowSums of these sub scales are to be categorized into 4 different Categories (1,2,3,4)like this:
Create the 'severity' variable: hat funktioniert !!! für Tabelle für ggplot!!
CTQ_SG <- data.frame(
Emotional_Abuse_Severity = cut(CTQ_TOTAL$EMO_ABUSE, breaks = c(0, 8, 12, 15, 25), labels = c(1, 2, 3, 4)),
Physical_Abuse_Severity = cut(CTQ_TOTAL$PYS_ABUSE, breaks = c(0, 7, 9, 12, 25), labels = c(1, 2, 3, 4)),
Emotional_Neglect_Severity = cut(CTQ_TOTAL$EMO_NEGL, breaks = c(0, 9, 14, 17, 25), labels = c(1, 2, 3, 4)),
Physical_Neglect_Severity = cut(CTQ_TOTAL$PYS_NEGL,breaks = c(0, 7, 9, 12, 25), labels = c(1, 2, 3, 4)),
Sexual_Abuse_Severity = cut(CTQ_TOTAL$SEX_ABUSE, breaks = c(0, 5, 7, 12, 25), labels = c(1, 2, 3, 4))
)
als Numerisch festlegen, wichtig, sonst klappt der rest nicht!!
CTQ_SG$Emotional_Abuse_Severity <- as.numeric(CTQ_SG$Emotional_Abuse_Severity)
CTQ_SG$Physical_Abuse_Severity<- as.numeric(CTQ_SG$Physical_Abuse_Severity)
CTQ_SG$Emotional_Neglect_Severity <- as.numeric(CTQ_SG$Emotional_Neglect_Severity)
CTQ_SG$Physical_Neglect_Severity <- as.numeric(CTQ_SG$Physical_Neglect_Severity)
CTQ_SG$Sexual_Abuse_Severity<- as.numeric(CTQ_SG$Sexual_Abuse_Severity)
#in Df zusätzlich ablegen
Df$CTQ_SG <- data.frame(
Emotional_Abuse_Severity = cut(CTQ_TOTAL$EMO_ABUSE, breaks = c(0, 8, 12, 15, 25), labels = c(1, 2, 3, 4)),
Physical_Abuse_Severity = cut(CTQ_TOTAL$PYS_ABUSE, breaks = c(0, 7, 9, 12, 25), labels = c(1, 2, 3, 4)),
Emotional_Neglect_Severity = cut(CTQ_TOTAL$EMO_NEGL, breaks = c(0, 9, 14, 17, 25), labels = c(1, 2, 3, 4)),
Physical_Neglect_Severity = cut(CTQ_TOTAL$PYS_NEGL,breaks = c(0, 7, 9, 12, 25), labels = c(1, 2, 3, 4)),
Sexual_Abuse_Severity = cut(CTQ_TOTAL$SEX_ABUSE, breaks = c(0, 5, 7, 12, 25), labels = c(1, 2, 3, 4))
)
Df$CTQ_SG <- as.numeric (Df$CTQ_SG)
Now I would love to look at the data by the following plot, in the way that I have the 5 subscales on the x-axis, see the bars of each sub scale and have the bars decided into the categories, 1,2,3,4… showing the amount of counts in each category. here is my plot:
plot_CTQ_total <- ggplot(data = Df, aes(x = CTQ_SG, y = after_stat(count), group = factor(CTQ_TOTAL),fill=factor(CTQ_TOTAL)))+
geom_bar(width = 0.5, position = 'stack', color = 'white') +
geom_text(aes(label = stat(count)), stat = 'count', position = position_stack(vjust = 0.5)) +
coord_flip() +
scale_fill_manual(values = c('cyan4', 'purple1', 'palevioletred3', 'peachpuff1'),
labels = c('Nicht bis minimal', 'Gering bis mäßig', 'mäßig bis schwer', 'schwer bis extrem')) +
labs(y = 'Häufigkeit', x = 'CTQ Subgruppen') +
labs(fill = 'Schweregrade') +
scale_x_continuous(breaks = 1:5,
labels = c("emotionaler Missbrauch", "körperlicher Missbrauch", "emotionale Vernachlässigung", "körperliche Vernachlässigung", "sexueller Missbrauch")) +
ggtitle('Schweregradeinteilung der CTQ Subgruppen') +
theme(plot.title = element_text(hjust = 0.6)) +
theme(axis.text.y = element_text(size = 8)) +
theme(axis.text.x = element_text(size = 10)) +
scale_y_continuous(breaks = seq(5, 45, 5))
print(plot_CTQ_total)
but the answer is always
Error in geom_bar()
:
! Problem while computing aesthetics.
Error occurred in the 1st layer.
Caused by error in check_aesthetics()
:
! Aesthetics must be either length 1 or the same as the data (186)
Fix the following mappings:
x
, group
, and fill
Run rlang::last_trace()
to see where the error occurred.
Warning messages:
1: In xtfrm.data.frame(x) : cannot xtfrm data frames
2: In xtfrm.data.frame(x) : cannot xtfrm data frames
3: In xtfrm.data.frame(x) : cannot xtfrm data frames
the variables have the same length (186) so that's not the point. any other ideas ? I would be very thankful !!