r - ggplot2: Is there a way to overlay a single plot to all facets in a ggplot -
i use ggplot , faceting construct series of density plots grouped factor. additionally, layer density plot on each of facets not subject constraints imposed facet.
for example, faceted plot this:
require(ggplot2) ggplot(diamonds, aes(price)) + facet_grid(.~clarity) + geom_density()
and have following single density plot layered on top of each of facets:
ggplot(diamonds, aes(price)) + geom_density()
furthermore, ggplot faceting best way this, or there preferred method?
one way achieve make new data frame diamonds2
contains column price
, 2 geom_density()
calls - 1 use original diamonds
, second uses diamonds2
. in diamonds2
there no column clarity
values used in facets.
diamonds2<-diamonds["price"] ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) + geom_density(data=diamonds2,aes(price),colour="blue")
update - suggested @briandiggs same result can achieved without making new data frame transforming inside geom_density()
.
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) + geom_density(data=transform(diamonds, clarity=null),aes(price),colour="blue")
another approach plot data without faceting. add 2 calls geom_density()
- in 1 add aes(color=clarity)
have density lines in different colors each level of clarity
, leave empty second geom_density()
- add overall black density line.
ggplot(diamonds,aes(price))+geom_density(aes(color=clarity))+geom_density()
Comments
Post a Comment