0

I am trying to plot mulitple lines between two paired observations, much like the example here:

example chart

Here is an example of my data.frame called "surp":

group   drug    data
BDD A   -1.1526
BDD A   -0.2916
BDD A   1.1954
BDD A   0.24379
BDD A   1.0958
BDD A   -0.45312
BDD B   0.42097
BDD B   -0.94172
BDD B   3.3395
BDD B   1.301
BDD B   0.25607
BDD B   0.32317
BDD B   2.621
HC  A   0.4826
HC  A   -0.57789
HC  A   2.4146
HC  A   0.13586
HC  A   0.9254
HC  A   0.41183
HC  A   -0.25771
HC  A   0.75699
HC  A   -0.86372
HC  A   1.2142
HC  A   0.33452
HC  A   -0.089335
HC  B   -3.048
HC  B   -0.19295
HC  B   0.43324
HC  B   -1.3974
HC  B   -1.4349
HC  B   2.2073
HC  B   0.71036
HC  B   -0.1725
HC  B   0.36907

Here is my example code:

ggplot(data = surp, aes(x = drug, y = data, group = group, colour =factor(group))) +
  geom_point()+
  geom_line(aes(group = drug)) 

However, that code just plots two vertical lines and not lines connecting each datapoint between A and B (the x axis) like the example that I have attached above.

  • could you please post your data using `dput` function?Just easily paste in your R console `dput(surp)` and copy the output. – Mal_a Aug 29 '17 at 07:03
  • Where is the variable which indicates which cases are connected together? I.e. you need an appropriate `group` variable. – Henrik Aug 29 '17 at 07:05
  • Thanks @Henrik, you were correct - I realised that my group variable was for the overall group, and, as geom_line will connect points according to the group aesthetic, I needed to specify the group variable according to the individual participant, not the group – Sally Grace Aug 30 '17 at 01:09

1 Answers1

0

The plot has only two lines due to only two groups in your data frame (BDD & HC)...

I think you should use something like that:

ggplot(data = surp, aes(x = drug, y = data, colour =factor(group))) +
  geom_point()+geom_line() + facet_wrap(~group)
Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • Thanks!! I ended up having to add a third column to my data frame with participant ID's (labelled 'participant') and I was able to get it working exactly how I wanted with the following code: `ggplot(mydat, aes(drug, data, group = participant, colour = group)) + geom_point() + geom_line() + facet_wrap(~group)` – Sally Grace Aug 30 '17 at 01:06