Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

If you specify axis limits in ggplot the outlying points are removed. This is fine for points, but you might want to plot lines that intersect with the specified range, but ggplot's range or xlim/ylim methods removes these. Is there another way to specify the plot axis range without removing outlying data?

e.g.

require(ggplot2)

d = data.frame(x=c(1,4,7,2,9,7), y=c(2,5,4,10,5,3), grp=c('a','a','b','b','c','c'))

ggplot(d, aes(x, y, group=grp)) + geom_line()

ggplot(d, aes(x, y, group=grp)) + geom_line() + scale_y_continuous(limits=c(0,7))

ggplot(d, aes(x, y, group=grp)) + geom_line() + ylim(0,7)

1 Answer

0 votes
by
edited by

To limit the axis without removing the data outside the limits, you need to set the limits inside the cartesian coordinate system instead of scale or setting ylim().

In your case:

require(ggplot2)

d = data.frame(x=c(1,4,7,2,9,7), y=c(2,5,4,10,5,3), grp=c('a','a','b','b','c','c'))

ggplot(d, aes(x, y, group=grp)) + 

  geom_line() + 

  coord_cartesian(ylim=c(3, 6))

Output:

image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...