0

I'm not skilled enough with ggplot2, but it seems the best library to create a slope chart to represent my results graphically. I have to represent a slope chart pointing to the differences in the results before and after the treatment. Be aware, I need to show that the two treatments produced almost the same results; that's why I'm looking for this chart. Thank you.

id<-c(1:5)
after<-c(35.69, 38.96, 33.10, 27.11, 36.55)
before<-c(35.96, 39.70, 33.85, 27.92, 38.38)
df<-data.frame(id, after,before)
Fede R
  • 21
  • 5
  • Does this answer your question? [Add regression line equation and R^2 on graph](https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph) – divibisan Jan 13 '22 at 16:07
  • Hello! StackOverflow is not really a coding service (i.e. let me give you my problem and you give me code), it's more for you to come with your coding problems and we'll help you solve them. You can look at [R graph gallery](https://www.r-graph-gallery.com/) for help (see: Line chart). – csgroen Jan 13 '22 at 16:07
  • @divibisan thank you but not really. I've to represent several lines. What I found on the internet is mostly the slope chart representing the changes based on x = date rather than pointing the differences between before and after. – Fede R Jan 13 '22 at 16:13

1 Answers1

2

Is this what you are after?

df <- data.frame(id, after,before) %>%
  pivot_longer(cols = c("after", "before"),
               names_to = "time")
ggplot(data = df,
       aes(x = time,
           y = value,
           group = id,
           color = factor(id))) +
  geom_line(size = 2,
            alpha = 0.5) +
  geom_point(size = 3)

enter image description here

cazman
  • 1,452
  • 1
  • 4
  • 11
  • Thanks cazman, this is was a was looking for! Amazing – Fede R Jan 13 '22 at 16:28
  • I need anyway the column "before/after" so ggplot2 can recognize where to place the value right? Just to be sure for the future, is not possible to add just the before and after columns, I have to think differently. – Fede R Jan 13 '22 at 16:41
  • 1
    Yes, having data in a long format is the preferred way of plotting, analyzing, manipulating, etc. – cazman Jan 13 '22 at 17:12