# Coord_cartesian( ) command is not working (R)

#1

Hello,
I’m doing the Exploratory Data Analysis course in Udacity®. In lesson 4 there’s a chapter on " Overlaying Summaries With Raw Data". It is required to plot a scatterplot, a mean line, and 3 quantile lines. Also the plot is to be zoomed in using the coord_cartesian function(coord_cartesian(xlim = c(13,70), ylim = c(0,1000)). To do the above, I used the following code:

ggplot(aes(x = age, y = friend_count), data = fb)+
coord_cartesian(xlim = c(13,70), ylim = c(0,1000))+
geom_point(alpha = 0.05,
position = position_jitter(h = 0),
color = ‘orange’)+
coord_trans(y = ‘sqrt’)+
geom_line(stat = ‘summary’, fun.y = mean)+
geom_line(stat = ‘summary’, fun.y = quantile, fun.args = list(probs = 0.1), color = ‘blue’,
linetype = 2)+
geom_line(stat = ‘summary’, fun.y = quantile, fun.args = list(probs = 0.9), color = ‘blue’,
linetype = 2)+
geom_line(stat = ‘summary’, fun.y = quantile, fun.args = list(probs = 0.5), color = ‘blue’)

The above code gives the following plot:

The problem I’m facing is the above code should limit(zoom) the graph on both axes((13,70) for x and (0,1000) for y) but it isn’t.
Where is the problem? Why isn’t the code providing desired results?

Regards

#2

@B.Rabbit

Hi there,

All you need to do is change the order of coord_cartesian. Below is the relevant code for that.

``````ggplot(aes(x = age, y = friend_count), data = fb)+
geom_point(alpha = 0.05,
position = position_jitter(h = 0),
color = 'orange')+
coord_trans(y = 'sqrt')+
geom_line(stat = 'summary', fun.y = mean)+
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = 0.1), color = 'blue',
linetype = 2)+
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = 0.9), color = 'blue',
linetype = 2)+
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = 0.5), color = 'blue')+coord_cartesian(xlim = c(13,70), ylim = c(0,1000))
``````

Below is the plotted figure for the above code.

Regards,

Neeraj