Use of last_plot and xlim() in R

r
datavisualization

#1

Hello,
I was following a tutorial on ggplot2 package in R and used the following commands to plot data

qplot(cut, data = diamonds)
qplot(carat, data = diamonds)
qplot(carat, data = diamonds, binwidth = 1)
qplot(carat, data = diamonds, binwidth = 0.1)
qplot(carat, data = diamonds, binwidth = 0.01)
resolution(diamonds$carat)
last_plot() + xlim(0, 3)

and I am getting an error saying->
Error: arguments imply differing number of rows: 313, 302

How to eliminate this error?
Edit: I used xlim(0,4) instead of xlim(0,3) and the error disappeared. What is the reason for this?

thanks.


#2

do ??xlim and you will get answer

Basically your first xlim gave an error because you were not giving enough space to the graph to be fit in

http://www.inside-r.org/packages/cran/ggplot2/docs/xlim

Convenience functions to set the limits of the x and y axis.