3

How can I break the the y axis not starting from 0 in my plot? I guess it can't be done with ggplot. Any other solutions?

I want to get this: enter image description here

My ggplot:

ggplot(allEvents %>% dplyr::filter(FixationID>1,FixationID<15), aes(FixationID, Fixation)) +
  geom_line(stat="summary", fun.y=mean, position="identity") + theme_bw() + stat_summary(fun.data=mean_se)+   
  labs (x = "Ordinal fixation number", y = "Fixation duration (s)") +  
  scale_y_continuous (name="Fixation duration (s) ", limits=c(0.1, 0.4)) + 
  scale_x_continuous (name="Ordinal fixation number", breaks=c(5,10,15,20)) + 
  geom_smooth(method=lm)

Excerpt from my data:

structure(list(Fixation = c(0.383, 0.185, NA, 0.312, NA, 0.328, 
NA, 0.259, NA, 0.335), FixationID = c(1, 2, NA, 3, NA, 4, NA, 
5, NA, 6)), .Names = c("Fixation", "FixationID"), row.names = c(NA, 
10L), class = "data.frame")
Tk Shmk
  • 111
  • 5
  • 2
    There was a very similar question before here: https://stackoverflow.com/questions/61077866/force-y-axis-to-start-at-0-insert-break-and-have-a-large-y-axis-using-ggplot. Briefly, ggplot2 doesn't facilitate discontinuous axes (breaks), but you could make them if you try hard. – teunbrand Apr 15 '20 at 22:01

1 Answers1

0

As mentioned by @teunbrand's comment, ggplot2 does not allow for axis breaks, s it is really tricky to get it done.

Alternatively, you can plot your graph in base r plot, and use axis.break function from plotrix package.

(NB: I used between function from dplyr to select the subset of data you are interested by. Your dataset is called df in my example).

library(dplyr) 
library(plotrix)

with(subset(df, between(FixationID,2,15)),
     plot(x = FixationID, y = Fixation, type = "b", ylim = c(0.1,0.4), pch = 16))
with(subset(df, between(FixationID,2,15)),
     abline(lm(Fixation~FixationID),col = "blue"))
axis.break(axis = 2, breakpos = 0.1, style="slash")

enter image description here

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32