2


I have a barplot with two factors and I need every bar to be in a different color. So far, I use one of the factors as the fill aesthetic:
ggplot(valenz, aes(x=group, y=mean, fill = condition))

Below is one example with fake data of how my barplots looks.

enter image description here

What I need is that color also differs depending on the group, not only the condition, e.g.:
group b, condition 1: light blue
group b, condition 2: dark blue
group c, condition 1: light green
group c, condition 2: dark green and so on...
So, using the 'alpha' option doesn't seem to do it.
Below is what the plot should approx. look like.

enter image description here

The rest should stay exactly how it is right now.
Does anyone know how to implement this? Thanks!

RamsesII
  • 404
  • 3
  • 10
  • 2
    Perhaps something akin to `aes(group, mean, fill = group, alpha = condition)`? – Axeman Apr 06 '18 at 15:07
  • *fake data of how my barplots looks* ... isn't that the desired look per your description below? – Parfait Apr 06 '18 at 15:12
  • Thanks for your ideas! Actually, I tried the 'alpha' option but I need the bars to really have different colors depending on their group, not just different amounts of transparency... – RamsesII Apr 07 '18 at 09:41
  • 1
    This is a duplicate, but the question has been incorrectly flagged. I think you can find your solution here: https://stackoverflow.com/questions/24435979/colouring-and-shading-texture-of-bars-according-to-grouping-ggplot2 – Michael Harper Apr 07 '18 at 17:33
  • @MikeyHarper that's exactly what I was looking for, thanks! – RamsesII Apr 09 '18 at 09:04

1 Answers1

4

I think this might help you move in the right direction :

library(tidyverse)

valenz <- data.frame(
  mean = c(6, 0, 2, 4, 3, 5, 4, 5),
  group = c("group a","group a", "group b","group b", "group c","group c","group d","group d"),
  condition = c("condition1", "condition2","condition1", "condition2","condition1", "condition2","condition1", "condition2")

)

ggplot (valenz, aes(x=group, y=mean, fill=factor(condition), alpha=group)) + 
  geom_bar(stat="identity",position="dodge")

source: R ggplot barplot; Fill based on two separate variables and some of my own tweaks.

Link_To_generated_Image

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19354395) – Rui Barradas Apr 06 '18 at 15:37
  • Oh ok thank you! I'm a new to Stack, will keep that in mind! – Anurag Kaushik Apr 06 '18 at 15:42
  • Don't worry, it happens all the time. Note where it says to include the relevant parts of the answer. It's good to have new users, good luck. – Rui Barradas Apr 06 '18 at 15:45
  • Thank you! Just made the changes, I hope this is better :) – Anurag Kaushik Apr 06 '18 at 16:04
  • Yes, it is. The thing that was now missing was code formating: you must put 4 spaces before each code line. To do this you can select the code and press Ctrl+K or use the foramting tools on top of the edit area. – Rui Barradas Apr 06 '18 at 16:49
  • Wow okay! Thanks again. Still learning as I go – Anurag Kaushik Apr 06 '18 at 16:53
  • Thank you, Anurag! I've already tried the 'alpha' option but I need the bars to really have different colors depending on their group, not just different amounts of transparency. – RamsesII Apr 07 '18 at 09:42