Limit ggplot2 axes without deleting data (external restrictions): increase

If you specify the axis limits in ggplot, the deleted points are deleted. This is good for points, but you may need to build lines that intersect with the specified range, but the ggplot range or xlim/ylim remove these. Is there any other way to specify the range of the axis of the graph without deleting external data?

eg.

 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) 
+73
r limits ggplot2 zoom
Sep 05 '14 at 11:45
source share
1 answer

Hadley explains this on page 99; 133 of his ggplot2 book (1st edition) or pp. 160 - 161 if you have a second edition

The problem is that, as you say limits inside the scale or the ylim parameter, the data is output as it limits the data. For true scaling (save all data) you need to set limits inside the Cartesian coordinate system. See http://docs.ggplot2.org/current/coord_cartesian.html for details

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

enter image description here

+117
Sep 05 '14 at 12:30
source share



All Articles