I have a dataset that is very small. It is this
activity <- data.frame(
stringsAsFactors = FALSE,
Gender = c("male", "female", "male", "female","male", "female", "male", "female"),
Season = c("Spring","Spring","Summer","Summer","Fall","Fall","Winter","Winter"),
n = c(450,280,890,720,300,250,300,250),
check.names = FALSE
)
dput(activity)
Gender | Season | n |
---|---|---|
Male | Spring | 450 |
Female | Spring | 280 |
Male | Summer | 890 |
Female | Summer | 720 |
Male | Fall | 300 |
Female | Fall | 250 |
Male | Winter | 300 |
Female | Winter | 250 |
I want to create a histogram to distinguish between male and female activity during season
ggplot(data = activity, mapping = aes( x = Season,
y = n)) geom_bar( mapping = aes(fill = Gender),
position = "dodge",
stat = "identity") ggtitle (Activity by Season and Gender)
This code above works well, but the colors are really ugly. I want blue for men and red for woman.
I gave that code below to change it
ggplot(data = activity, mapping = aes( x = Season,
y = n)) geom_bar( mapping = aes(fill = Gender), palette= c("blue","red"),
position = "dodge",
stat = "identity")
The code doesn't work. What I am doing wrong???
I had this from a programming book and there it works like this, but this code doesn't change the colors
CodePudding user response:
ggplot(data = activity, mapping = aes( x = Season,
y = n)) geom_bar( mapping = aes(fill = Gender),
position = "dodge",
stat = "identity") ggtitle (Activity by Season and Gender)
The code above works well.
No, it doesn't. You forgot to wrap Activity by Season and Gender
in quotes.
And this is what you were looking for.
ggplot(data = activity, mapping = aes( x = Season,y = n)) geom_bar( mapping = aes(fill = Gender), position = "dodge",stat = "identity") ggtitle ("Activity by Season and Gender") scale_fill_manual(values=c("red", "blue"))
I just added scale_fill_manual(values=c("red", "blue"))
to the end of the command.