Error: Fehler: Mapping should be created with `aes()` or `aes_()`

Hey everyone,
I am about to create a piechart with this code:

x %>%
ggplot(x, aes(x=1, y=Freq, fill=factor(korpus1))) +
geom_col(position = 'stack', width = 1) +
geom_text(aes(label = paste(round(Freq / sum(Freq) * 100, 1), "%"),
x = 1.3),
position = position_stack(vjust = 0.5)) +
theme_classic() +
theme(aes(plot.title = element_text(hjust=0.5)),
axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
labs(aes(fill = "korpus1",
x = NULL,
y = NULL,
title = "Tortendiagramm")) +
coord_polar("y")

an R just keeps telling me that "Mapping should be created with aes() or aes_()" and I really do not see what I did wrong. I really hope that someone can help me. Thank you!!

Hi. First of all, please read FAQ: What's a reproducible example ( reprex ) and how do I create one?

Your error appears because you trying to supply your data to ggplot() twice. You have to choose either you use pipe or define data inside ggplot() in a classical way.

# Bad
x %>%
ggplot(x, aes(x=1, y=Freq, fill=factor(korpus1))) +
geom_col(position = 'stack', width = 1)

# Good
x %>%
ggplot(aes(x=1, y=Freq, fill=factor(korpus1))) +
geom_col(position = 'stack', width = 1)

# Good
ggplot(x, aes(x=1, y=Freq, fill=factor(korpus1))) +
geom_col(position = 'stack', width = 1)

I also can spot two additional mistakes: you should not use aes() inside theme() and labs().
Perhaps, it should work in the following way, but I can't check without sample data.

ggplot(x, aes(x=1, y=Freq, fill=factor(korpus1))) +
  geom_col(position = 'stack', width = 1) +
  geom_text(aes(label = paste(round(Freq / sum(Freq) * 100, 1), "%"),
                x = 1.3),
            position = position_stack(vjust = 0.5)) +
  theme_classic() +
  theme(plot.title = element_text(hjust=0.5),
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  labs(fill = "korpus1",
       x = NULL,
       y = NULL,
       title = "Tortendiagramm") +
  coord_polar("y")

This topic was automatically closed 21 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.