help in ggplot barplot, I always get an error

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!!:wink:

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.
:information_source: 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)
:heavy_multiplication_x: 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 !!

This is a perennial favorite of mine for being uninformatively informative—probably because I've never internalized the idea of aesthetics as mainly meaning a geometry representing data or some auxiliary embellishment. Without a reprex (see the FAQ) to make this concrete, all I can do is to help understand the error.

is the offending line. Looking at the function signature

geom_bar(
  mapping = NULL,
  data = NULL,
  stat = "count",
  position = "stack",
  ...,
  just = 0.5,
  width = NULL,
  na.rm = FALSE,
  orientation = NA,
  show.legend = NA,
  inherit.aes = TRUE
)

The thing to keep an eye on is the last line—aesthetics are inherited from the ggplot() function.

As called, your function includes an optional width argument and an optional $\dots$ argument to pass color. The position argument was already the default. Everything else takes the default. So, nothing expressly changed the inherited aesthetic.

The data argument, Df controls which variable names in aes will be recognizable. aes is given CTQ_SG, which is what? It is a variable in Df, good enough, and it's been coerced with as.numeric. What type of variable? It is a data frame within a data frame, A data frame has a length equal to the number of its variables (columns). In this case, that is greater than one and, I am predicting, less than 186. I further predict that 186 is the number of rows of Df.

Provides the arguments that might be to blame and because Df$CTQ_SG which is provided as the x argument, that is what I'd check first. What single variable (not whole data frame) is to be displayed?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.