-3

I have two groups with one measurement variable.

I would like to plot them on one graph to see if they show a correlation or they overlap. The measurement for both group is in the same scale.

I thought of doing a scatter plot, but in this case, I thought it would just give me a straight line as I only have one measurement.

Could I get some ideas and suggestions please?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Kim So Yon
  • 85
  • 7
  • 2
    Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Mar 25 '20 at 06:57

1 Answers1

0

You can unstack the data.

set.seed(1234)
df <- data.frame(var = rnorm(200, 50, 10), gp = gl(2,100))

head(df)
       var gp
1 37.92934  1
2 52.77429  1
3 60.84441  1
4 26.54302  1
5 54.29125  1
6 55.06056  1

unstack(df)
          X1       X2
1   37.92934 54.14524
2   52.77429 45.25282
3   60.84441 50.65993
4   26.54302 44.97522
5   54.29125 41.74001
6   55.06056 51.66989

And then plot this.

library(ggplot2)
library(dplyr)

unstack(df) %>% ggplot(aes(x=X1, y=X2)) + 
  geom_point() + 
  geom_smooth(method="lm")
Edward
  • 10,360
  • 2
  • 11
  • 26