0
votes

When I add a color segmentation to ggplot and add geom_histogram using y=..density.., it produces a separate density for each color. I'm trying to get it to show a single density, and color code part of it.

If it were just a single chart, can get the right visual using y=..count... But, in a facet grid, the facets with smaller overall counts are visually too small:

ggplot(data=dataset, aes(x = myvariable, fill=factor(myfillfactor))) + facet_grid(cols = vars(myDimension1), rows = vars(myDimension2)) + geom_histogram(aes(y=..count..))

Using density makes the sizes more similar, but you can see that the 2 colors are actually 2 separate densities:

ggplot(data=dataset, aes(x = myvariable, fill=factor(myfillfactor))) + facet_grid(cols = vars(myDimension1), rows = vars(myDimension2)) + geom_histogram(aes(y=..count..)) + ylim(0,.2)

enter image description here

Any idea for having the density cover both colors? I also thought about keeping to a ..count.. and try to scale the facets, say by dividing each y value by the count of the group, but I don't know R well enough to see how to do that (I suppose that is what ..ncount.. does, but that produces separate densities too)

1

1 Answers

0
votes

So this answer may not solve the histogram issue, but if the goal is to get visually similar scaling of the histograms, you might also consider using facet_wrap(scales = "free_y"). For example:

# Make up some dummy data
df <- data.frame(x = c(rnorm(100, 1), rnorm(100, 2), rnorm(20, 1), rnorm(20, 2)),
                 cat = rep(c("A", "B", "A", "B"), c(100, 100, 20, 20)),
                 panel = rep(c("C", "D"), c(200, 40)))

# Free facet y scales
ggplot(df, aes(x, fill = cat)) +
  geom_histogram() +
  facet_wrap(~ panel, scales = "free_y")

enter image description here