Stripcharts of linked observations/ Plotting multiple lines with ggplot2 in R

689 views Asked by At

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.

1

There are 1 answers

1
Mal_a On

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)