0

I've been trying to compare two histograms by condition (either 1 or 0), but my code does not work. It creates a grey histogram that does not allow me to compare groups. Any clues here? Btw I'm working in an r dataframe.

ggplot(my_data, aes(x=GrandNeedMean, fill=Condition)) +
  geom_histogram(binwidth = 0.25, alpha=.5, position = "identity") +
  ggtitle("GrandNeedMean by condition histogram") 

Columns in dataframe

Histogram from current code

  • 3
    Hi. If you could share a sample of `my_data` that would really help. Does `fill = as.factor(Condition)` work? –  Jun 10 '22 at 13:34
  • Yeah sure. I've added the relevant columns, but I'll add more if that helps? I will try that - thanks! – Andreas Massey Jun 10 '22 at 13:36
  • 2
    Please don't post picture of data. See how to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). But as Adam pointed out, it appears your Condition is numeric. If you use `fill=factor(Condition)` then ggplot will treat the value as a categorical variable and show two distinct colors. Numeric scales won't work with the fill= aesthetic for histograms. – MrFlick Jun 10 '22 at 13:38
  • @Adam This might be a dumb question, but what more should I add from the "my_data" dataframe? – Andreas Massey Jun 10 '22 at 13:40
  • It worked! Thanks guys. I'll start to use reproducible code too - good advice:) – Andreas Massey Jun 10 '22 at 13:49

0 Answers0