-1

I have created a graph using ggplot2 in R and want to draw a line segment that's half the length between 2 points on the x-axis. The x-axis consists of dates and when I try to draw the segment it joins the 2 dates. How can I half the length and draw the line segment?

Sample Data:-

Sample Data

Sample Graph

  • Can you provide a reproducible example of the data you are using, or even a mock dataframe? – metaltoaster Sep 23 '20 at 13:46
  • Have you looked at the function ```geom_segment```? – SebSta Sep 23 '20 at 13:47
  • @SebSta yes I have but it creates a segment that joins the 2 days and I want half of that. – Spaceguy152 Sep 23 '20 at 13:56
  • @RobinTurkington the dataframe is basically just a bunch of dates and values:- – Spaceguy152 Sep 23 '20 at 13:58
  • @Spaceguy152 please read the section on [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Please don't just post screenshots of your data - use ``dput()`` to print out a sample of it. – user438383 Sep 23 '20 at 13:59
  • @Spaceguy152 I would assume your x axis is of the class ```Date```. Im pretty sure your starting and endponts can only be actual date. Try changing the x-axis data to ```posixct``` and the axis itself to ```datetime``` – SebSta Sep 23 '20 at 14:06

1 Answers1

1

Here is an example using the mtcars dataset

library(ggplot2)
sp <- ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()
sp + geom_segment(aes(x = 2, y = 15, xend = 3, yend = 15))

Which would give you this:-

enter image description here

You can customise the length with xend, which specifies where you want the line to end. However, with dates I am not so sure. If I am right in saying with your example, are you trying to place that line between 2 "DateTimes"? If so, would it not be easier if you changed the Date format to DMY_HM? Because I think this would make it easier to specify the exact length of the line.

metaltoaster
  • 380
  • 2
  • 15