0

I have the data of speeds associated different types of vehicles (lets say, Car, Motorcycle, Truck, Bus etc) and I want to show how the speed of these different types of vehicles differs when we compare to speed of car.

For example: In above mentioned case, 3 facets should be created showing density (comparison i.e. (Car vs Motorcycle, Car and Truck, Car and Bus)

Thanks in advance..!!!

  • 1
    There is nothing I know of in the `facet`s that can do this. One idea might be to duplicate the data you want to keep for comparison in each facet. Also if you want specific help it's very useful to post data and some code you have tried. See this post for ideas: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mike H. Mar 21 '17 at 13:13

1 Answers1

2

As mentioned in the comments, you need to create an additional dataframe that will display the same density plot for every member of your facet. Let's take the diamonds dataset as an example, where we'll take "Fair" cuts as our baseline:

# We use expand.grid to crate new data with same values for all levels
fair_data <- expand.grid(carat = diamonds$carat[diamonds$cut == "Fair"], 
                         cut = levels(diamonds$cut)[-1]) # We omit "Fair"

# Plug into ggplot
ggplot(subset(diamonds, cut != "Fair"), 
       aes(carat, colour = cut)) +
  geom_density() +
  geom_density(data = fair_data, colour = "black") +
  facet_wrap(~cut)

enter image description here

mtoto
  • 23,919
  • 4
  • 58
  • 71