CSC121 - R Console Monday March 5th --------------------------- > # Plotting (from lecture slides) > > plot (c(), xlim=c(0,2), ylim=c(1,5)) > plot (c(), xlim=c(0,2), ylim=c(1,5)) > # If plot window too small, you will get an error: Error in plot.new() : figure margins too large In addition: Warning messages: 1: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete 2: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 3: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state > plot (c(), xlim=c(0,2), ylim=c(1,5)) > > x <- c(3, 4, 5) > y <- c(1, 2, 3) > points(x, y, col="red", pch=20) > plot (c(), xlim=c(0,7), ylim=c(0,5)) > points(x, y, col="red", pch=20) > z <- c(5, 3, 1) > lines(z, col="green", lty="dotted") > lines(z, col="blue", lty="dotted") > points(z, col="blue", pch=20) > points(z, col="blue", pch=21) > x <- 1:10 > y <- x^2 > plot(x,y,xlim=c(0,11)) > text(x,y+2,paste("square of",x)) > source('~/.active-rstudio-document') > source('~/.active-rstudio-document') > source('~/.active-rstudio-document') > text(x,y+2,paste("square of",x)) TEXT_SHOW_BACKTRACE environmental variable. > > # Often in disciplines like physics or economics, you're going to want to model mathematical functions > # Now, the default x coordinates are the natural numbers: 1, 2, 3... > # But we know that functions are: > # continuous: we define functions over the real numbers > # Obviosuly, we can't store infinite real numbers in the computer > # But we also don't always want to just have the natural numbers as our x coordinate > # For example, what if we wanted to just the value of the function from 0 to 1 > # When you usually want to plot a function, you should define your x coordinates > > # For example: I want the values from 0 to 1, separated by 0.1 each > # 0, 0.1, 0.2, .... 1.0 > seq(0, 1, 0.1) [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 > # Let's say I want x^2, 0 <= x <= 1.0 > x <- seq(0, 1, 0.1) > x [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 > plot(x, x^2) > plot(x^2) > # Without specifying x as the x-coordinates, we get the natural numbers as our x-values, and not the original x values from the vector > plot(x, x^2) > plot(x, x^2, type="l") > x <- seq(0, 1, 0.01) > length(x) [1] 101 > plot(x, x^2, type="l") > x <- seq(0, 1, 0.2) > plot(x, x^2, type="l") > x <- seq(0, 1, 0.00001) > length(x) [1] 100001 > plot(x, x^2, type="l") >