I have a df with b-vals in columns and grouped (case/control) individuals in rows. And I am trying to get a plot that looks like this:
Is there an easy way to get this with ggplot?
I have a df with b-vals in columns and grouped (case/control) individuals in rows. And I am trying to get a plot that looks like this:
Is there an easy way to get this with ggplot?
I think you want to use geom_jitter
:
The jitter geom is a convenient shortcut for geom_point(position = "jitter"). It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.
First I created some reproducible random data. Here is the example:
df <- data.frame(beta_values = c(0.3, 0.31, 0.32, 0.29, 0.28, 0.5, 0.51, 0.52, 0.49, 0.48),
grouped = c("Casos_mds3", "Casos_mds3", "Casos_mds3", "Casos_mds3", "Casos_mds3", "Controles", "Controles", "Controles", "Controles", "Controles"))
df
#> beta_values grouped
#> 1 0.30 Casos_mds3
#> 2 0.31 Casos_mds3
#> 3 0.32 Casos_mds3
#> 4 0.29 Casos_mds3
#> 5 0.28 Casos_mds3
#> 6 0.50 Controles
#> 7 0.51 Controles
#> 8 0.52 Controles
#> 9 0.49 Controles
#> 10 0.48 Controles
library(ggplot2)
ggplot(df, aes(x = grouped, y = beta_values, color = grouped)) +
geom_jitter() +
labs(fill = "Groups", x = "", y = "Beta values") +
ggtitle("cg11400068") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5)) # center title
Created on 2022-07-12 by the reprex package (v2.0.1)