问题描述:

I have some data that looks like this:

> head(taus)

date window tau

1 2009-04-16 1 1

2 1994-09-13 1 3

3 2011-07-22 1 2

4 2003-09-05 1 1

5 2000-11-01 1 2

6 2008-02-12 1 1

There are a fixed number of windows. I can facet it like so:

ggplot(data=taus, aes(x=tau))

+ stat_bin(binwidth=10,col="black")

+ facet_wrap(~ window, ncol=4)

But is there a way to put all the graphs on one chart, giving each window a different color, and making them transparent so I can see them?

网友答案:

Something like:

ggplot(taus, aes(x=tau, fill=factor(window), group=window)) + 
   geom_bar(position='dodge', binwidth=10)

This way instead of faceting, you are using grouping and fill coloring. The factor(window) is to force ggplot to treat the integers in window as levels rather than a continuous variable.

Notice I also added position='dodge' to the geom_bar. That way the window groups aren't stacked.

网友答案:

this is what I ended up using, based on Justin's suggestion:

> ggplot(spx, aes(x=tau, fill=window, group=window, alpha=1/4)) +
+ geom_bar(position='identity', binwidth=5, drop=T) +
+ scale_y_log10()
网友答案:

this does essentially the same thing, but the alpha doesn't show up in the legend:

> ggplot(spx, aes(x=tau, group=window, fill=window)) + 
+     stat_bin(position='identity', binwidth=5, drop=T, alpha=0.3) +
+     scale_y_log10()
相关阅读:
Top