0

I have a factor of time that has two levels, admission and discharge. I'm using facet_grid to create four panels in which my continuous Y will be looked at by time. I want to be able to add a mean line to each of the two time levels in each panel. My problem is that the mean line spans the entire width of the panel and I'd like to shorten it to just remain within the area of the dots.

Here is the code:

plot <- ggplot(data.in, aes(x=Time, y=Y)) + geom_point()
plot <- plot + facet_grid(.~FacetGroup)
data_hline <- aggregate(data.in$Y~data.in$Time + data.in$FacetGroup, FUN=mean)
plot + geom_hline(data=data_hline, aes(yintercept=Y))

image

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • use `geom_segment` instead of `geom_hline` and supply the x-coordinates of your points to the `aes()` mapping – Julian_Hn Apr 02 '19 at 12:42
  • You might want to consider `geom_segment`. It will require you to make a data frame of the endpoints of the segments. – svenhalvorson Apr 02 '19 at 12:43
  • Thanks! That really works! Is there a way to create multiple `geom_segment` statements for each combination, or just go one by one? In other words, creating 8 separate `data.frame`s rather than just being able to do it in one go? – Mircea_cel_Batran Apr 02 '19 at 12:53
  • Have a look at the `group` aesthetic for `geom_segment()`. This allows you to separate your data into multiple plotted elements. – Mojoesque Apr 02 '19 at 13:55

0 Answers0