'Display explicit points on x axis for Density plots(ggplot2) in R

I have plotted a density plot using ggplot2 and as a next step I would like to explicitly highlight specific points(outliers) on x-axis. Below is my graph as far now: Density Plot

Below was my code to generate the same.:

p<-ggplot(df,aes(x=df$Infection_Risk)) + geom_density() + xlab("\n Infection 
  Risk") + ylab("Density \n")

Now, I want to show points c(7.7 7.6 7.8 1.3 1.3 1.4) explicitly on x axis with a different shape

Can someone please give an hint on this on what my next step should be?



Solution 1:[1]

You might consider plotting those using geom_point or geom_jitter (which adds some positioning noise to help distinguish overlapping points).

Here with fake data:

df <- tibble( Infection_Risk = rnorm(100, 4.5, 2))

outliers <- tibble(x = c(7.7, 7.6, 7.8, 1.3, 1.3, 1.4), y = 0)

ggplot(df,aes(x=Infection_Risk)) + geom_density() + xlab("\n Infection 
  Risk") + ylab("Density \n") +
  geom_jitter(data = outliers, aes(x,y), height = 0.01)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1