0

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: Grouped scatter plot

Is there an easy way to get this with ggplot?

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Yes. This could easily be achieved with `ggplot2`. But it's hard to help you any further without [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Jul 12 '22 at 19:07
  • Images are not a good way for posting data (or code). See [this Meta](https://meta.stackoverflow.com/a/285557/8245406) and a [relevant xkcd](https://xkcd.com/2116/). Can you post sample data in `dput` format? Please edit the question with the code you tried and with the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 20))`. (Note: `df` is the name of your dataset.) – Rui Barradas Jul 12 '22 at 19:09

1 Answers1

0

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)

Quinten
  • 35,235
  • 5
  • 20
  • 53