0

I am trying to change the fill colors of the bars based on their variable (male or female)

This is my code that works:

ggplot(sex_excercise_genhlth, aes(genhlth, prop_exer, fill=sex)) + 
geom_bar(stat="identity", colour="black", position="dodge" ) + labs(title="Proportions of males to females who exercised in last 30 days compared to their general health", x="General Health", y="Proportion Exercise")

plot

When I add the following code I get an error:

+scale_fill_manual(values=c(“blue”,“pink”))
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • 3
    Hi! what error did you get? And to make it reproducible, can you dput(sex_excercise_genhlth) and paste the output as part of your post? – StupidWolf Feb 08 '20 at 23:15
  • 3
    You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with creating a [reproducible example](https://stackoverflow.com/q/5963269). See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) – Tung Feb 08 '20 at 23:23
  • In your example here, you've got smart quotes in `c(“blue”,“pink”)`. Is that the case in your real code? – camille Feb 08 '20 at 23:39
  • Also, you didn't start a new line with the "+" did you? – Allan Cameron Feb 08 '20 at 23:54

1 Answers1

0

Here is a working example of how you might correctly use scale_fill_manual for your use case:

First I'll create some dummy data:

df <- data.frame(sex = rep(c("Male", "Female"), 5),
           place = rep(letters[1:5], each = 2),
           vals = rep(c(0.1, 0.2), 5) + seq(0.9, 0.45, -0.05))

Now the plot

ggplot(data = df, aes(x = place, y = vals)) + 
  geom_bar(stat = "identity", 
           colour = "black", 
           aes(fill = sex), 
           position = "dodge") +
   labs(title="Proportions of people exercising", 
        x="General Health", 
        y="Proportion Exercise") +
   scale_fill_manual(values = c("pink", "blue"))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • You don't have to set the labels; take that part out and your code still runs fine – camille Feb 08 '20 at 23:41
  • @camille thank you; you are right. I'll change this answer. – Allan Cameron Feb 08 '20 at 23:55
  • @camille I think adding "labels" does give you direct control over which level goes with which colour, though if you know the order of your factors you don't need it. – Allan Cameron Feb 09 '20 at 00:10
  • This worked and was super helpful! I'm a beginner so do you think you can explain to me what the you were telling the program to do in each line of the data.frame portion? – SabrinaReich Feb 09 '20 at 01:40
  • @SabrinaReich sure. `rep` just means repeat, so `rep(c("Male", "Female"), 5)` just repeats "Male", "Female", "Male", "Female" ... until `sex` is length 10. `letters` is a built-in vector of lowercase letters, so `letters[1:5]` just gives me a, b, c, d and e as characters. This time I repeat them using 'each' so the sequence will be a, a, b, b, c, c, d, d, e, e. Finally, I use `rep` to alternate between 0.1 and 0.2 (5 times each), and to this I add a sequence of decreasing numbers using `seq`. – Allan Cameron Feb 09 '20 at 01:58
  • @SabrinaReich If the answer has solved your problem please consider marking it as accepted to help other users with similar problems to find an answer. Thanks – Allan Cameron Feb 09 '20 at 02:00