1

I have the following code:

Figure1plot <- ggplot(Figure1data, aes(x = ngirls, y = nowtot, fill = party, col = party))
  + stat_summary(fun = mean, geom = "bar", inherit.aes = TRUE) 
  + facet_grid(rows = vars(totchi), cols = vars(party), margins = "party") 
  + scale_fill_manual(values = c("Democrat" = "blue", "Republican" = "red", "(all)" = "black"),
  breaks = c("Democrat", "Republican", "(all)"), aesthetics = c("color", "fill")) 
  + xlab("Number of female children") 
  + ylab("Mean NOW score")

which produces the follwing bar chart:

enter image description here

How do I apply different shadings for the different values for the x-variable (number of female children), so that the bars have different shadings of blue, red, and black? I've tried changing the x and y variables into factors and integers, without success.

Additionally, how do I add another legend for the number of daughters?

Many thanks in advance!

Nelizor96
  • 27
  • 4

1 Answers1

2

What about adding transparency in your barchart based on x variable ? You can pass alpha = ngirls in your aes and add scale_alpha_manual to customize it:

ggplot(Figure1data, aes(x = ngirls, y = nowtot, 
                        fill = party, col = party, 
                        alpha = ngirls))+ 
  stat_summary(fun = mean, geom = "bar", inherit.aes = TRUE) +
  facet_grid(rows = vars(totchi), cols = vars(party), margins = "party") +
  scale_fill_manual(values = c("Democrat" = "blue", "Republican" = "red", "(all)" = "black"),
                    breaks = c("Democrat", "Republican", "(all)"), aesthetics = c("color", "fill")) +
  xlab("Number of female children") +
  ylab("Mean NOW score")+
  scale_alpha_manual(values = c(0,0.2,0.6,0.8))

Does it answer your question ?

If not, can you provide a reproducible example of your dataset by following this guide: How to make a great R reproducible example

dc37
  • 15,840
  • 4
  • 15
  • 32