Question about Ggplot fill=

Hi. Why does the following not graph the data separately by gender? Thank you.

library(ggplot2)
id <- c(1,2,3,4,5,6,7,8,9,10)
gender <- c(0,0,0,0,0,1,1,1,1,1)
toys<- c(4,2,0,2,2,2,2,8,7,2)
df<- data.frame(cbind(id,gender,toys))
df
ggplot(data = df, aes(x=toys, fill=gender)) +
geom_bar(color="white") +
labs(title = "Distribution of Number of Toys",
y = "Count",
x = "Number of toys",
fill = "Gender")

It seems both x and fill have to be factors.

id <- c(1,2,3,4,5,6,7,8,9,10)
gender <- c(0,0,0,0,0,1,1,1,1,1)
toys<- c(4,2,0,2,2,2,2,8,7,2)

df<- data.frame(cbind(id,gender,toys))
df$gender <- factor(df$gender)
df$toys <- factor(df$toys)
library(ggplot2)
ggplot(data = df, aes(x=toys, fill=gender)) +
  geom_bar(color="white") +
  labs(title = "Distribution of Number of Toys",
       y = "Count",
       x = "Number of toys",
       fill = "Gender")

FJCC, that solve dit. Thank you.

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