0

How do I change fill pattern or color group by group? My current code only changes the outline; I want to change the fill pattern or fill color group (not outline) by sex but also keep my fill gradient which is by Eye.

colors <- c("Green", "Blue", "Hazel", "Brown")
data <- data.frame(HairEyeColor)
data$Eye <- as.numeric(factor(data$Eye, labels = 1:4))
data <- data[c(5,6,12,15,17,22,27,28), ]

ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) + 
  geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex)) +
  scale_fill_continuous(low = "blue", high = "green")

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
George
  • 69
  • 1
  • 9
  • Can you explain better the final graph? – Al14 Aug 23 '17 at 23:17
  • Right so as you can see in the bar plot that was generated for this question by Henrik (thanks for the formatting), it's difficult to tell the difference between male and female at a glance, as it is only separated by differently colored outlines (hard to tell, even with thicker outlines). I'd like to know if possible how to change either color gradient by gender or fill pattern by gender. – George Aug 23 '17 at 23:20
  • You can map `alpha` (transparency) to sex. It would probably work better if eye color was discrete - I'm a bit confused why you're treating eye color as continuous anyway. See the example [in my question here](https://stackoverflow.com/q/20129299/903061). – Gregor Thomas Aug 23 '17 at 23:50
  • This is a sample dataset; I have a continuous data set to map as a gradient per bar. I'd prefer not use alpha to differentiate my groups as I have up to four groups. – George Aug 24 '17 at 00:18
  • 2
    So what would your desired final output look like *specifically*? When you say "change color gradient/fill pattern by gender", what are you envisioning each bar looking like? – Brian Aug 24 '17 at 00:46
  • I'd like to have a different color fill gradient per gender instead of the differently colored outlines, if not fill pattern. Sorry if I'm not clear. – George Aug 24 '17 at 14:00

1 Answers1

1

Bar plot from ggplot2 package does not support fill pattern at the moment (and as far as i know it is not possible with other packages neither). However there are few solution that are going to help spot the difference in sex and eye easily which you can consider:

1.Using different (lighter) fill colours,thicker bar boundaries and theme_bw():

 ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) + 
  geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex), size=2) +
  scale_fill_continuous(low = "white", high = "grey") + theme_bw()

enter image description here

  1. Merging two columns: Sex and Eye to get the new factor column which is going to be used as a fill argument:

    data$Sex_Eye <- paste(data$Sex, data$Eye, sep="_") ggplot(data, aes(x = Hair, y = Freq, fill = Sex_Eye)) + geom_bar(stat = "identity", position = position_dodge()) + theme_bw()

enter image description here

  1. Using geom_jitter() instead of geom_bar() and setting up shape argument as Sex:

ggplot(data, aes(x = Hair, y = Freq, colour = Eye, shape = Sex)) + geom_jitter(size=5) + scale_colour_continuous(low = "blue", high = "green") + theme_bw()

enter image description here

Mal_a
  • 3,670
  • 1
  • 27
  • 60