Reputation: 5
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..!!!
Upvotes: 0
Views: 1007
Reputation: 24178
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)
Upvotes: 2