# Line graph before and after treatment

#1

Hi everyone,
I have a data one 100 individuals containing 3 columns.

1. ID
2. Weight before treatment
3. Weight after treatment
As en example you could create a table using this:
``````Data <- data.frame( ID=seq(1:100), Before=sample(80:100,size=100, replace=TRUE), After=sample(60:100,size=100,  replace=TRUE))
``````

How can i produce a graph similiar to the one in the following link:
https://www.researchgate.net/profile/Kim_Harrison/publication/262786874/figure/fig3/AS:195802115907586@1423694270260/Line-graph-of-24-hour-cough-count-before-and-after-8weeks-treatment-with-acid.png

much appreciated
Kind regards
AM

#2
``````data_one %>% # assuming that's the name of your data
gather('stage', 'value', 2:3) %>% #rename 'stage', 'value', respectively to suit your case
ggplot(aes(x = stage, y = value, group = ID)) +
geom_line() +
geom_point()
``````

#3

Thanks Soedr,
I am still a newbi and struggling with some details here, specifically the gather function
So if I have a dataset like this:

``````Data <- data.frame( ID=seq(1:100), Before=sample(80:100,size=100, replace=TRUE), After=sample(60:100,size=100, replace=TRUE))
``````

how would you do that with this table?
Thanks a million

#4

You could also do it using `geom_segment`:

``````library(ggplot2)

data <- data.frame(
ID=seq(1:10),
Before=sample(80:100,size=10, replace=TRUE),
After=sample(60:100,size=10, replace=TRUE)
)

ggplot(data) +
geom_segment(aes(x = 1, xend = 2, y = Before, yend = After)) +
theme_bw() +
scale_x_discrete(
breaks = c("1", "2"),
labels = c("Before", "After"),
limits = c(1, 2)
) +
labs(y = "")
``````

Created on 2018-08-02 by the reprex package (v0.2.0).

I reduced it to 10 observations because the dataset with 100 observations was pretty much unreadable when trying to use this type of graph, IMO

#5

Perfect, thank you very much

#6

I'd recommend not naming dataframes Data (or data), since it may cause conflicts with r's native data() function. Setting that aside, do the following:

``````library(tidyr)
library(ggplot2)

Data <- data.frame(
ID=seq(1:100),
Before=sample(80:100,size=100, replace=TRUE),
After=sample(60:100,size=100,  replace=TRUE)
)

Data %>%
gather('stage', 'value', 2:3)  %>% # we combine the two columns (at column index 2 until 3) into one ('stage') that contains the column names from your original data (Before, After) and one (value) that contains the values for each observation
ggplot(aes(x = stage, y = value, group = ID)) +
geom_line() +
geom_point()
``````