Density curve and jitter plot

Dear colleagues,

I want to create a density curve with individual dots.
The model is from this manuscript:

image

I have found a few tutorials, and also one nice package (ggridges) to plot this.
Here is my result. However, this package does only work with a y-value (e.g. 2 categories), but does not work if I just want to plot it for a single gorup.

My questions are:

  • Can I plot single density curves using the ggridges package? I do not want multiple behind each other.
  • Can I plot an ordinary density curve using ggplot2 including a jitter plot for individual dots?

Thank you very much

# dataframe
set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5)))
  )

# Packages
library(ggplot2)
library(ggridges)

ggplot(df, aes(x = weight, y = sex)) +
  geom_density_ridges(jittered_points = TRUE)

Hi @Georg_Semmler,

Is this what you are trying to do?

# Packages
library(ggplot2)
library(ggridges)

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5)))
)

ggplot(df, aes(x = weight, y = 1, fill = sex)) +
  geom_density_ridges(jittered_points = TRUE, alpha = 0.4) +
  theme_classic()
#> Picking joint bandwidth of 1.53

1 Like

Thank you very much :slight_smile:
Does this also work with just one group?

Yours,
Georg Semmler

Yes, you just need to remove fill = sex.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.